1.To give idea that each process has the process ID.
2.To clearify that each child process again run with its own process ID.
3.To use the functions : getpid() ( to get its process id) and getppid()
( to get the process id of its parent)
Source Code:
#include
#include
int main()
{
int pid,ppid;
pid=getpid();
ppid=getppid();
printf("The process ID is:%d\n",pid);
printf("The parent process ID is:%d\n",ppid);
return 0;
}
Output:
2.Objectives:
1.To give idea that a process can create a new sub process.
2.To clearify that a system can create a new process. (When it calls next process then the new process will have new process id).
3.To use the function : system()
Source Code:
#include
int main()
{
int val;
val=system("ls -l");
printf("Your request has been Done!\n");
return ;
}
Output:
3.Objectives:
1.To give idea that a process can create a new child process.
2.To clearify that when fork() is used it creates two versions of the program that is written inside it (So we can realize real child process using fork();)
3.To use the function : fork()
Source Code:
#include
int main()
{
printf("This is the test for fork:\n");
printf("This is to demonstrate the fork:\n");
fork();
printf("Hi! Everybody\n");
printf("My name is Mr. R.:\n");
return 0;
}
3.Objectives:
1.To give idea that a process can create a new child process.
2.To clearify that when fork() is used it creates two versions of the program
3. that is written inside it (So we can realize real child process using fork();)
4.To use the function : fork()
5.To use the functions : getpid() ( to get its process id) and getppid()
( to get the process id of its parent)
Source Code:
#include
#include
int main()
{
int pid;
printf("The main program process programm Id is:%d\n",(int)getpid());
pid=fork();
if(pid!=0)
{
printf("This is the parent process with PID:%d\n",(int)getpid());
printf("The Child PProcess ID is:%d\n",pid);
}
else
printf("This is the child process with id:%d\n",(int)getpid());
return 0;
}
Output:
#include
#include
int main()
{
int pid;
pid=fork();
if(pid==0)
{
for(;;)
printf("Child Proces\n");
}
else if(pid>0)
{
for(;;)
printf("Parent Process\n");
}
Objective: To use threads in a program
Source code:
// to print the sum up to the given number using thread.
#include
#include
#include
void * sum(void * n)
{
int sum=0;
int i;
int *no=(int*)n; // typecasting void*n into int n
for( i=0;i<=*no;i++) sum=sum+i; printf("The sum of the numbers is :%d",sum); printf("\n"); } int main() { int n; pthread_t thread1_id; printf("\nEnter the number upto which you want the sum:"); scanf("%d",&n); pthread_create(&thread1_id,NULL,&sum,&n); return 0; } Write a program for Strict Alternation for critical region problem and analyze the output. Objective: To demonstrate the strict alternation. To create new thread ,execute and wait for particular thread to finish. To use -lpthread in linking the program. Source code: #include
#include
#include
#include
void *thread1f(void * arg);
void *thread2f(void * arg);
int turn=1;
int main()
{
pthread_t thid1;
pthread_t thid2;
pthread_create(&thid1, NULL, &thread1f, NULL);
pthread_create(&thid2, NULL, &thread2f, NULL);
pthread_join(thid1, NULL);
pthread_join(thid2, NULL);
return 0;
}
void *thread1f(void * arg)
{
int a=0;
while(a++<20) { while(turn!=1) fputc('b', stderr); turn=0; } } void *thread2f(void * arg) { int b=0; while (b++<20) { while(turn!=0) fputc('a', stderr); turn=1; } } Output 3Write the problems found in strict alternation and develop new version of the program using Peterson's algorithm. Objective: To implement Peterson’s algorithm for handling race condition. To create new thread ,execute and wait for particular thread to finish. Source code: #include
#include
#include
#include
#define N 2
enum{FALSE,TRUE};
int turn=0;
int interested[N];
void leave_region(int);
void enter_region(int);
void *thread1(void *arg)
{
int i;
for(i=1;i<20000;i++) printf("Non-Critical section or first process:\n"); enter_region(0); for(i=0;i<20000;i++) printf("Critical section for First process:\n"); leave_region(0); } void *thread2(void *arg) { int i; for(i=0;i<10000;i++) printf("Non-Critical section for Second process:\n"); enter_region(1); for(i=0;i<20000;i++) printf("Critical section for second process:\n"); leave_region(1); } void enter_region(int process) { int i; int other; other=1-process; interested[process]=TRUE; turn=process; while(turn==process && interested[other]==TRUE); } void leave_region(int process) { interested[process]=FALSE; } int main() { pthread_t th1; pthread_t th2; pthread_create(&th1,NULL,&thread1,NULL); pthread_create(&th2,NULL,&thread2,NULL); pthread_join(th1,NULL); pthread_join(th2,NULL); } Output: Write the program equivalent to to ‘cp’ command of UNIX with the name ‘filecopy’ Source Code: #include
#include
#define max 100
main(int argc, char* argv[])
{
FILE *fs,*fd;
char line[max];
if (argc>3)
{
printf("Too many parameters to create a file. Use correct syntax");
exit(1);
}if (argc<3)
{
printf("Too few parameters to create a file. Use correct syntax");
exit(1);
}
else
{
if ((fs=fopen(argv[1],"r"))==NULL)
{
printf("\nCan't find the source file '%s'. \n Confirm it again.\n",argv[1]);
exit(1)
}
else
{
if((fd=fopen(argv[2],"r"))!=NULL)
{
printf("\nDestination file already exists.");
printf("\n 0 file(S) copied");
exit(1);
}
else
{
fd=fopen(argv[2],"w");
while(fgets(line,max,fs)!=NULL)
fputs(line,fd);
printf("\nOne file is copied\n");
fclose(fs);
fclose(fd);
}
}
}
return 0;
}