home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / os2 / programm / 4256 < prev    next >
Encoding:
Text File  |  1992-08-17  |  2.3 KB  |  85 lines

  1. Newsgroups: comp.os.os2.programmer
  2. Path: sparky!uunet!mcsun!sun4nl!fwi.uva.nl!gene.fwi.uva.nl!heederik
  3. From: heederik@fwi.uva.nl (Robbert Heederik)
  4. Subject: simulated fork(), semaphore questions
  5. Message-ID: <1992Aug17.114502.11080@fwi.uva.nl>
  6. Keywords: fork semaphore OS2
  7. Sender: news@fwi.uva.nl
  8. Nntp-Posting-Host: nat.fwi.uva.nl
  9. Organization: FWI, University of Amsterdam
  10. Date: Mon, 17 Aug 1992 11:45:02 GMT
  11. Lines: 72
  12.  
  13. Hi,
  14.  
  15. Some time ago, someone was complaining that OS/2 doesn't provide a
  16. fork() call. What about the following program? It isn't *exactly*
  17. a fork(), but it comes close.
  18.  
  19. ------------------------------fork.c---------------------------------
  20. /* compile with:
  21.     gcc -c fork.c
  22.     gcc -o fork.exe fork.o -los2
  23. */
  24. #include <stdio.h>
  25. #include <os2.h>
  26.  
  27. int forked=0;
  28.  
  29. main()
  30. {
  31.     TID        fork;
  32.  
  33.     if(!forked) {
  34.     printf("forking\n");
  35.     forked=1;
  36.     if(DosCreateThread(&fork,(PFNTHREAD)main,0L,0L,8192L)) {
  37.         perror("DosCreateThread()");
  38.         exit(1);
  39.     }
  40.     }
  41.     if (fork==0) {        /* CHILD */
  42.     printf("CHILD\n");
  43.     } else {            /* PARENT */
  44.     printf("PARENT\n");
  45.     }
  46.     while(1);    /* a difference with fork()...if the parent dies, 
  47.            the children die. So...don't let the parent die. */
  48.     return 0;
  49. }
  50. ----------------------------EOF fork.c----------------------------------
  51.  
  52. Another question: 
  53. I tried to let two different process communicate with the use of a semaphore.
  54. The server contained code like this:
  55. ...
  56.     if(DosCreateEventSem(SEM_NAME,&sem,0L,0L)) {
  57.     perror("DosCreateEventSem()");
  58.     exit(1);
  59.     }
  60. ...
  61. and the client:
  62. ...
  63.     if(DosOpenEventSem(SEM_NAME,&sem)) {
  64.     perror("DosOpenEventSem()");
  65.     exit(1);
  66.     }
  67. ...
  68. (with SEM_NAME #defined to "\\SEM\\MYSEM.SEM")
  69.  
  70. I got the error "Bad file number"
  71. What did I wrong? 
  72. Creating a semaphore with SEM_NAME #def'd to NULL and creating
  73. two threads for the server and the clientprocedures worked well.
  74. (I only have a OS/2 1.0 technical reference (SIC!), so I haven't got
  75. any docs about the new semaphoresystem...)
  76.  
  77. Thanks in advance,
  78.     Robbert
  79.  
  80. -- 
  81. //// Robbert Heederik  //     __________           // heederik     // OS/2 ?  /
  82. /// Heiligeweg 26/28  // ----[__________|-------[ // @fwi.uva.nl  // OS/2 !? //
  83. // 1012 XR Amsterdam //  A shot a day            // University   // OS/2 !! ///
  84. / The Netherlands   // keeps the tourists away  // of Amsterdam // OS/2.  /////
  85.