home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.os2.programmer
- Path: sparky!uunet!mcsun!sun4nl!fwi.uva.nl!gene.fwi.uva.nl!heederik
- From: heederik@fwi.uva.nl (Robbert Heederik)
- Subject: simulated fork(), semaphore questions
- Message-ID: <1992Aug17.114502.11080@fwi.uva.nl>
- Keywords: fork semaphore OS2
- Sender: news@fwi.uva.nl
- Nntp-Posting-Host: nat.fwi.uva.nl
- Organization: FWI, University of Amsterdam
- Date: Mon, 17 Aug 1992 11:45:02 GMT
- Lines: 72
-
- Hi,
-
- Some time ago, someone was complaining that OS/2 doesn't provide a
- fork() call. What about the following program? It isn't *exactly*
- a fork(), but it comes close.
-
- ------------------------------fork.c---------------------------------
- /* compile with:
- gcc -c fork.c
- gcc -o fork.exe fork.o -los2
- */
- #include <stdio.h>
- #include <os2.h>
-
- int forked=0;
-
- main()
- {
- TID fork;
-
- if(!forked) {
- printf("forking\n");
- forked=1;
- if(DosCreateThread(&fork,(PFNTHREAD)main,0L,0L,8192L)) {
- perror("DosCreateThread()");
- exit(1);
- }
- }
- if (fork==0) { /* CHILD */
- printf("CHILD\n");
- } else { /* PARENT */
- printf("PARENT\n");
- }
- while(1); /* a difference with fork()...if the parent dies,
- the children die. So...don't let the parent die. */
- return 0;
- }
- ----------------------------EOF fork.c----------------------------------
-
- Another question:
- I tried to let two different process communicate with the use of a semaphore.
- The server contained code like this:
- ...
- if(DosCreateEventSem(SEM_NAME,&sem,0L,0L)) {
- perror("DosCreateEventSem()");
- exit(1);
- }
- ...
- and the client:
- ...
- if(DosOpenEventSem(SEM_NAME,&sem)) {
- perror("DosOpenEventSem()");
- exit(1);
- }
- ...
- (with SEM_NAME #defined to "\\SEM\\MYSEM.SEM")
-
- I got the error "Bad file number"
- What did I wrong?
- Creating a semaphore with SEM_NAME #def'd to NULL and creating
- two threads for the server and the clientprocedures worked well.
- (I only have a OS/2 1.0 technical reference (SIC!), so I haven't got
- any docs about the new semaphoresystem...)
-
- Thanks in advance,
- Robbert
-
- --
- //// Robbert Heederik // __________ // heederik // OS/2 ? /
- /// Heiligeweg 26/28 // ----[__________|-------[ // @fwi.uva.nl // OS/2 !? //
- // 1012 XR Amsterdam // A shot a day // University // OS/2 !! ///
- / The Netherlands // keeps the tourists away // of Amsterdam // OS/2. /////
-