some pthread test progs
authortridge <>
Wed, 3 Nov 2004 16:49:23 +0000 (16:49 +0000)
committertridge <>
Wed, 3 Nov 2004 16:49:23 +0000 (16:49 +0000)
pthreads/Makefile [new file with mode: 0644]
pthreads/euid.c [new file with mode: 0644]
pthreads/locking.c [new file with mode: 0644]
pthreads/tls.c [new file with mode: 0644]
pthreads/yield.c [new file with mode: 0644]

diff --git a/pthreads/Makefile b/pthreads/Makefile
new file mode 100644 (file)
index 0000000..4e9e371
--- /dev/null
@@ -0,0 +1,16 @@
+CFLAGS=-Wall
+LIBS=-lpthread
+
+all: tls euid locking
+
+tls: tls.o
+       $(CC) -o tls tls.o $(LIBS)
+
+euid: euid.o
+       $(CC) -o euid euid.o $(LIBS)
+
+locking: locking.o
+       $(CC) -o locking locking.o $(LIBS)
+
+clean:
+       rm -f *~ euid tls *.o
diff --git a/pthreads/euid.c b/pthreads/euid.c
new file mode 100644 (file)
index 0000000..419bbad
--- /dev/null
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <pthread.h>
+
+/*
+  create a thread with initial function fn(private)
+*/
+static int thread_start(void *(*fn)(void *private), void *private)
+{
+       pthread_t thread_id;
+       pthread_attr_t thread_attr;
+       int rc;
+
+       pthread_attr_init(&thread_attr);
+       pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+       rc = pthread_create(&thread_id, &thread_attr, fn, private);
+       pthread_attr_destroy(&thread_attr);
+       return rc;
+}
+
+/*
+  main thread fn
+*/
+static void *thread_main(void *private)
+{
+       int tid = *(int *)private;
+       uid_t uid, uid2;
+
+       uid = geteuid();
+       seteuid(tid);
+       uid2 = geteuid();
+
+       printf("tid %d  uid=%d uid2=%d\n", tid, (int)uid, (int)uid2);
+       return NULL;
+}
+
+/* lock a byte range in a open file */
+int main(int argc, char *argv[])
+{
+       int tid=1;
+       uid_t uid;
+
+       thread_start(thread_main, &tid); tid++;
+       thread_start(thread_main, &tid); tid++;
+       thread_start(thread_main, &tid); tid++;
+       thread_start(thread_main, &tid); tid++;
+       thread_start(thread_main, &tid); tid++;
+       thread_start(thread_main, &tid); tid++;
+
+       uid = geteuid();
+       if (uid != 0) {
+               printf("euid failed\n");
+       }
+
+       return 0;
+}
diff --git a/pthreads/locking.c b/pthreads/locking.c
new file mode 100644 (file)
index 0000000..c8c8f77
--- /dev/null
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <pthread.h>
+#include <fcntl.h>
+
+/*
+  create a thread with initial function fn(private)
+*/
+static int thread_start(void *(*fn)(void *private), void *private)
+{
+       pthread_t thread_id;
+       pthread_attr_t thread_attr;
+       int rc;
+
+       pthread_attr_init(&thread_attr);
+       pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+       rc = pthread_create(&thread_id, &thread_attr, fn, private);
+       pthread_attr_destroy(&thread_attr);
+       return rc;
+}
+
+/*
+  main thread fn
+*/
+static void *thread_main(void *private)
+{
+       int tid = *(int *)private;
+       int fd;
+       struct flock lock;
+       int ret;
+
+       if (tid == 1) {
+               fd = open("lock.tst", O_RDWR|O_CREAT, 0644);
+       } else {
+               fd = open("lock.tst", O_RDWR);
+       }
+
+       lock.l_type = F_WRLCK;
+       lock.l_whence = SEEK_SET;
+       lock.l_start = 0;
+       lock.l_len = 4;
+       lock.l_pid = 0;
+
+       ret = fcntl(fd, F_SETLK, &lock);
+
+       if (tid == 1) {
+               if (ret != 0) {
+                       perror("locking/tid1");
+                       exit(1);
+               }
+               sleep(2);
+       } else {
+               if (ret != -1) {
+                       printf("error: tid2 lock succeeded!\n");
+                       exit(1);
+               }
+       }
+
+       return NULL;
+}
+
+/* lock a byte range in a open file */
+int main(int argc, char *argv[])
+{
+       int tid=1;
+
+       thread_start(thread_main, &tid); tid++;
+       sleep(1);
+       thread_start(thread_main, &tid); tid++;
+
+       return 0;
+}
diff --git a/pthreads/tls.c b/pthreads/tls.c
new file mode 100644 (file)
index 0000000..53bbd10
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <pthread.h>
+
+/*
+  create a thread with initial function fn(private)
+*/
+static int thread_start(void *(*fn)(void *private), void *private)
+{
+       pthread_t thread_id;
+       pthread_attr_t thread_attr;
+       int rc;
+
+       pthread_attr_init(&thread_attr);
+       pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+       rc = pthread_create(&thread_id, &thread_attr, fn, private);
+       pthread_attr_destroy(&thread_attr);
+       return rc;
+}
+
+static __thread int tls;
+static int non_tls;
+
+/*
+  main thread fn
+*/
+static void *thread_main(void *private)
+{
+       tls++;
+       non_tls++;
+       printf("tls=%d non_tls=%d\n", tls, non_tls);
+       return NULL;
+}
+
+/* lock a byte range in a open file */
+int main(int argc, char *argv[])
+{
+       thread_start(thread_main, NULL);
+       thread_start(thread_main, NULL);
+       thread_start(thread_main, NULL);
+       thread_start(thread_main, NULL);
+       thread_start(thread_main, NULL);
+
+       printf("tls=%d non_tls=%d\n", tls, non_tls);
+
+       if (tls != 0 || non_tls != 5) {
+               printf("tls failed\n");
+               exit(1);
+       } else {
+               printf("tls OK\n");
+       }
+       return 0;
+}
diff --git a/pthreads/yield.c b/pthreads/yield.c
new file mode 100644 (file)
index 0000000..0cd651a
--- /dev/null
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <pthread.h>
+
+
+/*
+  create a thread with initial function fn(private)
+*/
+static int thread_start(void *(*fn)(void *private), void *private)
+{
+       pthread_t thread_id;
+       pthread_attr_t thread_attr;
+       int rc;
+
+       pthread_attr_init(&thread_attr);
+       pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+       rc = pthread_create(&thread_id, &thread_attr, fn, private);
+       pthread_attr_destroy(&thread_attr);
+       return rc;
+}
+
+static int xx;
+const int nthreads = 2;
+
+/*
+  main thread fn
+*/
+static void *thread_main(void *private)
+{
+       int n = (int)private;
+
+       while (1) {
+               if (xx % nthreads == n) {
+                       n++;
+               }
+               pthread_yield();
+       }
+
+       return NULL;
+}
+
+/* lock a byte range in a open file */
+int main(int argc, char *argv[])
+{
+       int i;
+
+       for (i=0;i<nthreads;i++) {
+               thread_start(thread_main, i);
+       }
+       sleep(10);
+       return 0;
+}