Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

Nithin

1 min read

#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

You may like these posts

  • #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++…
  • #include<stdio.h>#include <fcntl.h>int main(){int fd1 = open("foo.txt", O_RDONLY);if (fd1 < 0){perror("c1");exit(1);}printf("opened the fd = % d\n", fd1);// Using cl…
  • Connect Volunteers with Nonprofits /* CSS styles */ body { font-family: Arial, sans-serif; background-color: #f2f2f2; } …
  •  ....................................,,,,,,,...................................................................................................................................…
  •  [18/07, 10:39 am] Nagarjuna Cse❤️: #include<stdio.h>#include<conio.h>struct process { int at,ts,st,ft,wait,ts2,ta; float nta;}p[20];main() { int i,…
  • ChatBot ::-webkit-scrollbar{ width:0; } *{ margin:0; padding:0; box-sizing:border-box; } body{ width:100vw; he…

Post a Comment