Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

CS604 Idea Solution

View previous topic View next topic Go down

GMT + 3 Hours CS604 Idea Solution

Post by plhr60 Fri Nov 04, 2011 11:56 am

for q=2
take an idea........

Code:
#include <stdio.h>  /* printf, stderr, fprintf */
#include <sys/types.h> /* pid_t */
#include <unistd.h>  /* _exit, fork */
#include <stdlib.h>  /* exit */
#include <errno.h>  /* errno */
 
int main(void)
{
  pid_t  pid;
 
  /* Output from both the child and the parent process
    * will be written to the standard output,
    * as they both run at the same time.
    */
  pid = fork();
  if (pid == -1)
  { 
      /* Error:
      * When fork() returns -1, an error happened
      * (for example, number of processes reached the limit).
      */
      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
  }
 
  if (pid == 0)
  {
      /* Child process:
      * When fork() returns 0, we are in
      * the child process.
      * Here we count up to ten, one each second.
      */
      int j;
      for (j = 0; j < 10; j++)
      {
        printf("child: %d\n", j);
        sleep(1);
      }
      _exit(0);  /* Note that we do not use exit() */
  }
  else
  {
      /* Parent process:
      * When fork() returns a positive number, we are in the parent process
      * (the fork return value is the PID of the newly created child process).
      * Again we count up to ten.
      */
      int i;
      for (i = 0; i < 10; i++)
      {
        printf("parent: %d\n", i);
        sleep(1);
      }
      exit(0);
  }
  return 0;
}
plhr60
plhr60
Monstars
Monstars

Aquarius Horse
Posts : 536
Join date : 2011-10-20
Age : 34
Drunk

Character sheet
Experience:
CS604 Idea Solution Left_bar_bleue33/500CS604 Idea Solution Empty_bar_bleue  (33/500)

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum