- Set a default PCM volume so that something can be heard when driver is
[AROS.git] / test / clib / flock.c
blob39aeded0ece29ad19022934e31bb449cb861cffd
1 #include <sys/file.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <exec/types.h>
7 #include <proto/dos.h>
8 #include <dos/dos.h>
9 #include <proto/exec.h>
10 #include "test.h"
12 char *tmpname;
14 #define ITERATIONS 50
15 #define NPROCS 10
17 LONG entry()
19 struct Task *this = FindTask(NULL);
20 Wait(SIGBREAKF_CTRL_C);
21 int *counter = this->tc_UserData;
22 int i;
23 struct Library *aroscbase;
25 aroscbase = OpenLibrary("arosc.library", 0);
26 if(!aroscbase)
27 return -1;
29 for(i = 0; i < ITERATIONS; i++)
31 int fd = open(tmpname, 0);
32 if(!flock(fd, LOCK_EX))
34 int tmp = *counter;
35 Delay(1);
36 *counter = tmp + 1;
37 printf("\rprogress: %.2f%%", *counter * 100.0 / (NPROCS * ITERATIONS));
38 flock(fd, LOCK_UN);
40 else
42 close(fd);
43 return -1;
45 close(fd);
48 CloseLibrary(aroscbase);
49 return 0;
52 int fd;
54 int main()
56 tmpname = mktemp("T:flockXXXXXX");
57 int fd = open(tmpname, O_CREAT);
58 TEST((fd != -1));
60 TEST((flock(fd, LOCK_SH|LOCK_NB) == 0));
61 TEST((flock(fd, LOCK_UN) == 0));
63 TEST((flock(fd, LOCK_EX|LOCK_NB) == 0));
64 TEST((flock(fd, LOCK_UN) == 0));
66 TEST((flock(fd, LOCK_SH) == 0));
67 TEST((flock(fd, LOCK_UN) == 0));
69 TEST((flock(fd, LOCK_EX) == 0));
70 TEST((flock(fd, LOCK_UN) == 0));
72 /* Create NPROCS processes increasing counter ITERATIONS times in an ugly
73 way */
74 int counter = 0;
75 struct Process *procs[NPROCS];
76 APTR ids[NPROCS];
77 struct TagItem tags[] =
79 { NP_Entry, (IPTR) entry },
80 { NP_Name, (IPTR) "flocker" },
81 { NP_Output, (IPTR) Output() },
82 { NP_CloseOutput, (IPTR) FALSE },
83 { NP_UserData, (IPTR) &counter },
84 { NP_NotifyOnDeath, (IPTR) TRUE },
85 { TAG_DONE, 0 }
88 int i;
89 for(i = 0; i < NPROCS; i++)
91 procs[i] = CreateNewProc(tags);
92 TEST((procs[i]));
93 ids[i] = GetETask(procs[i])->et_UniqueID;
94 Signal(procs[i], SIGBREAKF_CTRL_C);
97 for(i = 0; i < NPROCS; i++)
99 ChildWait(ids[i]);
100 ChildFree(ids[i]);
102 putchar('\n');
104 TEST((counter == NPROCS * ITERATIONS));
106 cleanup();
107 return OK;
110 void cleanup()
112 close(fd);
113 remove(tmpname);