#include<stdio.h>
main()
{
int i,a=1,h=2,n;
printf("\n Enter the no of jobs");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(a==1)
{
printf("processing %d......! \n", i+1);
a++;
}
if(h>1)
{
if(i+2<=n)
{
printf("\n processing %d.....! \n",i+2);
}
printf("\n Process %d Enters Critical section", i+1);
printf("\n Process %d Leaves Critical section", i+1);
}
h+1;
}
}
-------------------------------------------------------------
Experiment. 2 – Write a program to create new process and to overlay an executable
binary image on an existing process.
// C program to illustrate use of fork() &
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(){
pid_t pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1){
printf("can't fork, error occured\n");
exit(EXIT_FAILURE);
}
else if (pid == 0){
printf("child process, pid = %u\n",getpid());
printf("parent of child process, pid = %u\n",getppid());
char * argv_list[] = {"ls","-lart","/home",NULL};
execv("ls",argv_list);
exit(0);
}
else{
printf("Parent Of parent process, pid = %u\n",getppid());
printf("parent process, pid = %u\n",getpid());
if (WIFEXITED(status) && !WEXITSTATUS(status))
printf("program execution successful\n");
else if (WIFEXITED(status) && WEXITSTATUS(status)) {
if (WEXITSTATUS(status) == 127) {
printf("execv failed\n");
}
else
printf("program terminated normally,"
" but returned a non-zero status\n");
}
else
printf("program didn't terminate normally\n");
}
else {
printf("waitpid() failed\n");
}
exit(0);
}
return 0;
}
Output:
parent process, pid = 11523
child process, pid = 14188
Program execution successful