training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

ex-8.2.c (578B)


      1 /*
      2  * Advanced Programming in the UNIX(r) Environment
      3  *
      4  * Exercise 8.2
      5 */
      6 
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <sys/types.h>
     10 #include <unistd.h>
     11 
     12 pid_t vfunc(void);
     13 
     14 int
     15 main(void)
     16 {
     17    pid_t pid;
     18 
     19    if( (pid = vfunc()) == -1 ) {
     20       perror("vfunc");
     21       return EXIT_FAILURE;
     22    }
     23 
     24    printf("[%d] Returning..\n", pid);
     25 
     26    return EXIT_SUCCESS;
     27 } /* eof main() */
     28 
     29 pid_t
     30 vfunc(void)
     31 {
     32    pid_t pid = vfork();
     33 
     34 #if !defined(__linux__)
     35    if( pid > 0 )
     36       sleep(2); /* Parent: wait a bit to child returns first */
     37 #endif
     38 
     39    return pid;
     40 } /* eof vfunc() */
     41