Back to index

glibc  2.9
tst-sem3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <semaphore.h>
00022 #include <stdint.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>
00028 #include <sys/wait.h>
00029 
00030 
00031 int
00032 main (void)
00033 {
00034   size_t ps = sysconf (_SC_PAGESIZE);
00035   char tmpfname[] = "/tmp/tst-sem3.XXXXXX";
00036   char data[ps];
00037   void *mem;
00038   int fd;
00039   sem_t *s;
00040   pid_t pid;
00041   char *p;
00042 
00043   fd = mkstemp (tmpfname);
00044   if (fd == -1)
00045     {
00046       printf ("cannot open temporary file: %m\n");
00047       exit (1);
00048     }
00049 
00050   /* Make sure it is always removed.  */
00051   unlink (tmpfname);
00052 
00053   /* Create one page of data.  */
00054   memset (data, '\0', ps);
00055 
00056   /* Write the data to the file.  */
00057   if (write (fd, data, ps) != (ssize_t) ps)
00058     {
00059       puts ("short write");
00060       exit (1);
00061     }
00062 
00063   mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00064   if (mem == MAP_FAILED)
00065     {
00066       printf ("mmap failed: %m\n");
00067       exit (1);
00068     }
00069 
00070   s = (sem_t *) (((uintptr_t) mem + __alignof (sem_t))
00071                & ~(__alignof (sem_t) - 1));
00072   p = (char *) (s + 1);
00073 
00074   if (sem_init (s, 1, 1) == -1)
00075     {
00076       puts ("init failed");
00077       exit (1);
00078     }
00079 
00080   if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
00081     {
00082       puts ("1st wait failed");
00083       exit (1);
00084     }
00085 
00086   errno = 0;
00087   if (TEMP_FAILURE_RETRY (sem_trywait (s)) != -1)
00088     {
00089       puts ("trywait succeeded");
00090       exit (1);
00091     }
00092   else if (errno != EAGAIN)
00093     {
00094       puts ("trywait didn't return EAGAIN");
00095       exit (1);
00096     }
00097 
00098   *p = 0;
00099 
00100   puts ("going to fork now");
00101   pid = fork ();
00102   if (pid == -1)
00103     {
00104       puts ("fork failed");
00105       exit (1);
00106     }
00107   else if (pid == 0)
00108     {
00109       /* Play some lock ping-pong.  It's our turn to unlock first.  */
00110       if ((*p)++ != 0)
00111        {
00112          puts ("child: *p != 0");
00113          exit (1);
00114        }
00115 
00116       if (sem_post (s) == -1)
00117        {
00118          puts ("child: 1st post failed");
00119          exit (1);
00120        }
00121 
00122       puts ("child done");
00123     }
00124   else
00125     {
00126       if (TEMP_FAILURE_RETRY (sem_wait (s)) == -1)
00127        {
00128          printf ("parent: 2nd wait failed: %m\n");
00129          exit (1);
00130        }
00131 
00132       if (*p != 1)
00133        {
00134          puts ("*p != 1");
00135          exit (1);
00136        }
00137 
00138       puts ("parent done");
00139     }
00140 
00141   exit (0);
00142 }