Showing posts with label system programming. Show all posts
Showing posts with label system programming. Show all posts

Saturday, September 1, 2007

shared files program in C

shared files program in C

/* shared file example 1 */


#include <fcntl.h>
#include <stdio.h>


main()
{
int fp;
char buff[20];
fp=open("testfile",O_RDONLY);
if(fork()==0)
{
read(fp,buff,10);
buff[10]=0;
puts(buff);
}

else
{
wait(0);
read(fp,buff,10);
buff[10]=0;
puts(buff);
}

}

/*shared file example 2 */

#include <fcntl.h>
#include <stdio.h>


main()
{
FILE *fp;
char buff[20];
fp=fopen("testfile","r");
if(fork()==0)
{
fgets(buff,10,fp);
buff[10]=0;
puts(buff);
}

else
{
wait(0);
fgets(buff,10,fp);
buff[10]=0;
puts(buff);

}

}

/*lock file */


#include <fcntl.h>
#include <stdio.h>


main()
{
int fd;
fd=open("Lockset",O_RDWR);
lockf(fd,F_LOCK,0);
printf("Process %d locked the file \n",getpid());
if(fork()==0)
{
lockf(fd,F_LOCK,0);
printf("Process %d locked the file \n",getpid());
printf("Child Process is over\n");
}

else
{
sleep(5);
printf("Process %d is over\n",getpid());

}

}

semaphore program in C

Semaphore program in C

/* basic semaphore example 1 */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>


main()
{
int sid;
int key,nsem;
key=(key_t)0x30;
nsem=1;
sid=semget(key,nsem,IPC_CREAT0777);
printf("\nSemaphore was created with the id = %d\n",sid);

}

/* basic semaphore example 2*/

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>


main()
{
int sid;
int key,nsem;
key=(key_t)0x30;
nsem=1;
sid=semget(key,nsem,IPC_CREAT0666);
printf("\nSemaphore was created with the id = %d\n",sid);
sleep(15);
semctl(sid,0,IPC_RMID,0);
}


/* readers */

#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <stdio.h>


void sem_wait (int);
void sem_signal(int);


main(){
int rdcnt,wrt,mutex;
key_t rdkey=0x20;
key_t wrkey=0x30 ;

rdcnt = semget(rdkey,1,0666IPC_CREAT);
wrt = semget(wrkey,1,0666IPC_CREAT);
mutex = semget(0x40 ,1,0666IPC_CREAT);

semctl(mutex,0,SETVAL,1);
printf("\nStarting to Read.....");
sem_wait(mutex);
sem_signal(rdcnt);
sleep(10);
printf("\nReading Finished");
sem_wait(rdcnt);
sem_signal(mutex);

}


void sem_wait(int id){
struct sembuf buf = { 0,-1,0};
semop(id,&buf,1);
}

void sem_signal(int id){
struct sembuf buf = { 0,1,0};
semop(id,&buf,1);


}

forking i.e examples in C (fork program)

forking i.e example in C (fork program)
===============================


/*forking example 1 */
#include<sys/types.h>
main()
{
int i;
int j;
fork();

printf("\n%d\n",getpid());
printf("\n%d\n",getppid());
getchar();

}

/*forking example 2 */
main()
{
int i=fork();
if(i==0)
{
printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
else
{
printf("I'm the Parent.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
wait();
}
exit(0);
}


/*forking example 3 */

main()
{
int i=fork();
if(i==0)
{
printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());

sleep(5);

printf("I'm the child.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
else
{
sleep(2);
printf("I'm the Parent.My PID is %d\n",getpid());
printf("And my parent's PID is %d\n",getppid());
}
}

/*forking example 4 */

#include<sys/types.h>
main()
{
int j;
int pid=fork();
if(pid==0)
for(j=0;j<30000;j++)
printf("\n%10d\tChild",j);

else
for(j=0;j<30000;j++)
printf("\n%10d\tParent",j);
}


/*forking example 5*/

#include<sys/types.h>
main()
{
int j;
if(fork()==0)
{
printf("\nFirst Child Executing");
for(j=0;j<10000;j++)
;
printf("\nFirst Child is dying now\n" );
}
else
{
if(fork()==0)
{
printf("Second Child Executing");
for(j=0;j<10000;j++)
;
printf("\nSecond Child is dying now\n" );
}
else
{

printf("\nI'm the BOSS here\n");
printf("\nThe first child to die is %d\n",wait(0));

for(j=0;j<10000;j++)
;
printf("\nThe Second child to die is %d\n",wait(0));

printf("But nothing is in my hand!\n");
}

}

}

/*forking example 6*/

main()
{
int pid;
int i=10;
pid=fork();
if(pid==0)
{
printf("\nThe value of I in child is \t%d\n",i);
i+=10;
printf("\nThe (inc)value of I in child is \t%d\n",i);

}

else
{
wait(0);
printf("\nThe value of I in parent is \t%d\n",i);
}
}

/*forking example 7*/

main()
{
int i=10;
if(fork()==0)
printf("\nThe address of i in child is %X \n",&i);
else
printf("\nThe address of i in parent is %X \n",&i);

}


/*forking example 8*/

#include<sys/types.h>
main()
{
if(fork()>0)
{
printf("I'm the parent\n");
sleep(10);
}


}