úåëðéåú ãåâîà ùì fork

 

 /* forkdemo1.c - first use of fork() */

 

#include <stdio.h>

 

int main()

{

  printf("Before fork:\n");

  fork();

  printf("After fork.\n");

  return 0;

} /* main */

 

_______________________________

 

math:/home3/ronn/OS > cc forkdemo1.c

math:/home3/ronn/OS > ./a.out

Before fork:

After fork.

After fork.

math:/home3/ronn/OS >  

 

/* forkdemo2.c  */

 

#include <stdio.h>

 

int main()

{

  printf("Before fork: My pid = %d\n", getpid());

  fork();

  printf("After fork:  My pid = %d\n", getpid());

  return 0;

} /* main */

 

________________________________

 

math:/home3/ronn/OS > cc forkdemo2.c

math:/home3/ronn/OS > ./a.out

Before fork: My pid = 21370

After fork:  My pid = 21371

After fork:  My pid = 21370

math:/home3/ronn/OS >        

 

/* forkdemo3.c  */

 

#include <stdio.h>

 

int main()

{

  int pid;

 

  pid = getpid();

  printf("Before fork: My pid = %d\n", getpid());

  fork();

  printf("After fork:  My pid = %d, pid = %d\n", getpid(), pid);

  return 0;

} /* main */

 

_______________________________________________

 

math:/home3/ronn/OS > cc forkdemo3.c

math:/home3/ronn/OS > ./a.out

Before fork: My pid = 21458

After fork:  My pid = 21459, pid = 21458

After fork:  My pid = 21458, pid = 21458

math:/home3/ronn/OS >   

 

/* forkdemo5.c  */

 

#include <stdio.h>

 

int main()

{

  int pid1, pid2;

 

  pid1 = getpid();

  printf("Before fork: My pid = %d\n", getpid());

 

  pid2 = fork();

 

  if ( pid2 != 0)

   printf(" I am the parent! getpid() = %d, pid = %d, id = %d\n",

            getpid(), pid1, pid2);

  else

   printf(" I am the child!  getpid() = %d, pid = %d, id = %d\n",

            getpid(), pid1, pid2);

 

  return 0;

} /* main */

 

______________________________

 

 

math:/home3/ronn/OS > cc forkdemo5.c

math:/home3/ronn/OS > ./a.out

Before fork: My pid = 22066

 I am the child!  getpid() = 22067, pid = 22066, id = 0

 I am the parent! getpid() = 22066, pid = 22066, id = 22067

math:/home3/ronn/OS > 

 

 

úåëðéú ãåâîà äëåììåú âí àú äô÷åãåú execl, wait, system

 

/* date0.c - Use the system command */

 

#include <stdio.h>

 

void main()

{

  printf("Here comes the date:\n");

  system("date");

  printf("That was the date.\n");

}

 

_______________________________________________________________

 

mathcs4:/home4/ronn/OS > cc date0.c

mathcs4:/home4/ronn/OS > a.out

Here comes the date:

Mon Dec 23 18:37:46 IST 1996

That was the date.

mathcs4:/home4/ronn/OS >

 

_________________________________________________________________

 

 

/* date1.c - call system date routine using execl */

 

#include <stdio.h>

void main()

{

  printf("Here comes the date:\n");

  execl("/bin/date", "date", 0);

  fprintf(stderr, "execl failed.\n");

}

 

___________________________________________________________

 

mathcs11:/home4/ronn/OS > cc date1.c

mathcs11:/home4/ronn/OS > a.out

Here comes the date:

Sun Jan  7 17:32:52 WET 1996

mathcs11:/home4/ronn/OS >

 

____________________________________________________________

 

 

/* date2.c - first use of fork() */

 

#include <stdio.h>

void main()

{

  printf("Here comes the date:\n");

  fork();

  execl("/bin/date", "date", 0);

  printf("That was the date.\n");

}

 

__________________________________________________

 

mathcs11:/home4/ronn/OS > cc date2.c

mathcs11:/home4/ronn/OS > a.out

Here comes the date:

Sun Jan  7 17:33:39 WET 1996

Sun Jan  7 17:33:39 WET 1996

mathcs11:/home4/ronn/OS >

 

/* date4.c - Third use of fork() */

 

#include <stdio.h>

void main()

{

  int id;

  printf("Here comes the date:\n");

 

  if ( ( id =  fork() ) == 0 )

  {  /* select child process */

     printf("PID is %d and ID is %d.\n", getpid(), id);

     execl("/bin/date", "date",0);

  }

 

  /* parent process */

  printf("PID is %d and ID is %d.\n", getpid(), id);

  printf("That was the date.\n");

}

__________________________________________________________________

 

mathcs11:/home4/ronn/OS > cc date4.c

mathcs11:/home4/ronn/OS > a.out

Here comes the date:

PID is 20439 and ID is 0.

PID is 19414 and ID is 20439.

That was the date.

Sun Jan  7 17:52:13 WET 1996

mathcs11:/home4/ronn/OS >

 

__________________________________________________________________

__________________________________________________________________

 

/*  date5.c - Use of fork() and wait() */

 

#include <stdio.h>

void main()

{

  int id, wid, status;

  printf("Here comes the date:\n");

 

  if ( ( id =  fork() ) == 0 )

  {  /* select child process */

     printf("PID is %d and ID is %d.\n", getpid(), id);

     execl("/bin/date", "date",0);

  }

  wid = wait(&status);

  printf("That was the date.\n");

  printf("PID is %d and ID is %d.\n", getpid(), id);

  printf("wid = %d and status = %d\n", wid, status);

}

 

__________________________________________________________________

 

mathcs11:/home4/ronn/OS > cc date5.c

mathcs11:/home4/ronn/OS > a.out

Here comes the date:

PID is 23794 and ID is 0.

Sun Jan  7 17:54:55 WET 1996

That was the date.

PID is 20465 and ID is 23794.

wid = 23794 and status = 0

mathcs11:/home4/ronn/OS >

 

/*  date6.c - Use of fork() and wait() */

 

#include <stdio.h>

void main()

{

  int id, wid, status;

  printf("Here comes the date:\n");

 

  switch (  id =  fork() )

  {  /* select child process */

 

   case -1:

           perror("fork");

           exit(1);

 

   case 0:

     execl("/bin/date", "date",0);

       perror("execl");

       exit(1);

 

   default:

          break;

 

  } /* switch */

 

  wid = wait(&status);

  printf("That was the date.\n");

 

} /* main */

 

 

___________________________________________

 

 

math2:/usr/users/home7/ronn/OS > cc date6.c

math2:/usr/users/home7/ronn/OS > a.out

Here comes the date:

Mon Jan 21 15:51:42 IST 2002

That was the date.

math2:/usr/users/home7/ronn/OS >