Lab- Assessment-1 Name: S.Haritha Slot: L49+L50
Reg.No: 17MIS0236 Faculty: Deepa.K
1)Parent child process creation using fork( ) and exec() system call Checking the Process Identifier Assigning new task to child Providing the path name and program name to exec() Synchronizing Parent and child process using wait() Code: #include<stdio.h> #include #include<stdlib.h> #include<sys/wait.h> int main(void) { int pid; // child variable int status;//Status of the child printf("hi\n"); char *args[] = {"sai./EXEC",&status}; execl(args[0],args); printf("Executing File"); //Parent Process Identifier and with their address printf("I am the parent process and pid is : %d.\n",getpid()); printf(" I am just befeor use of fork\n"); //Assinging task to fork() pid = fork(); //Assign task to the child to print printf("here I am just after forking\n"); //Child process Identifier with their address
if (pid == -1) //checking for error { wait(&status); printf("I am waiting for the child"); } if (pid == 0) printf(" I am the child process and pid is :%d.\n", getpid()); else printf(" I am the parent process and pid is:%d./n",getpid()); }
Output:
2)Write a Program to create parent and child process. Assign the task of calculating the Fibonacci Series to parent and checking whether a number is Armstrong Number or not to Child.
Code: #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include int main() { int pid=fork(); if(pid==0) { int number; int sum=0; int rem=0; int temp; printf("enter a number"); scanf("%d",&number); temp=number; while(number!=0) { rem=number%10; sum=sum+(rem*rem*rem); number=number/10; } if(sum==temp) printf("armstrong"); else printf("no"); } else { int first=0; int second=1; int next; int c; int n; printf("Enter the number of terms\n"); scanf("%d",&n); printf("%d\n",first); printf("%d\n",second); for(c=1;c<=n;c++)
{ next=first+second; first=second; second=next; printf("%d\n",next); } } }
Output: