leobegin2016
leobegin2016
采纳率100%
2017-05-07 16:55 阅读 871

下面代码哪出错了啊?linux的多线程

30

#include
2 #include
3 #include
4 #include
5 #include
6
7 void *thread_function(void *arg);
8 void print(pid_t);
9 sem_t *sem;
10 int val;
11 pthread_t a_thread;
12
13 int main(int argc,char *argv[])
14 {
15 int n = 0;
16
17 if(argc != 2)
18 {
19 printf("please input a file name!\n");
20 exit(1);
21 }
22 sem = sem_open(argv[1],O_CREAT,0644,3);
23
24 while(n++<5)
25 {
26 if((pthread_create(&a_thread,NULL,thread_function,NULL)) != 0)
27 {
28 perror("Thread creation failed");
29 exit(1);
30 }
31 }
32
33 pthread_join(a_thread,NULL);
34 sem_close(sem);
35 sem_unlink(argv[1]);
36 }
37
38 void *thread_function(void *arg)
39 {
40 sem_wait(sem);
41 print();
42 sleep(1);
43 sem_post(sem);
44 printf("I'm finished,my tid is %d\n",pthread_self());
45 }
46
47 void print()
{
49 printf("I get it,my tid is %d\n",pthread_self());
50 sem_getvalue(sem,&val);
51 printf("Now the value have %d\n",val);
52 }

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    iamoyjj 欧阳嘉 2017-05-08 09:43

    OK try this

    compile with gcc thd.c -o thd -lpthread -lrt

     #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <semaphore.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void *thread_function(void *arg);
    void print();
    sem_t *sem;
     int val;
     pthread_t a_thread;
    
     int main(int argc,char *argv[])
     {
     int n = 0;
    
     if(argc != 2)
     {
     printf("please input a file name!\n");
     exit(1);
     }
     sem = sem_open(argv[1],O_CREAT,0644,3);
    
     while(n++<5)
     {
     if((pthread_create(&a_thread,NULL,thread_function,NULL)) != 0)
     {
     perror("Thread creation failed");
     exit(1);
     }
     }
    
     pthread_join(a_thread,NULL);
     sem_close(sem);
     sem_unlink(argv[1]);
     }
    
     void *thread_function(void *arg)
     {
     sem_wait(sem);
     print();
     sleep(1);
     sem_post(sem);
     printf("I'm finished,my tid is %d\n",pthread_self());
     }
    
     void print()
     {
     printf("I get it,my tid is %d\n",pthread_self());
     sem_getvalue(sem,&val);
     printf("Now the value have %d\n",val);
     }
    
    
    点赞 评论 复制链接分享
  • iamoyjj 欧阳嘉 2017-05-07 23:55

    try compile this with

    gcc sem.c -o sem -lrt

     #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <semaphore.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void print(pid_t);
    
    sem_t *sem;
    int val;
     int main(int argc,char *argv[])
     {
     int n = 0;
     if(argc != 2);
     {
     printf("please input afile name!\n");
     exit(1);
     }
     sem = sem_open(argv[1],O_CREAT,0644,2);
    
     while(n++<5)
     {
     if(fork()==0)
     {
     sem_wait(sem);
     print(getpid());
     sleep(1);
     sem_post(sem);
     printf("I'm finished,my pid is %d\n",getpid());
     return 0;
     }
     }
    
     wait();
     sem_close(sem);
     sem_unlink(argv[1]);
     exit(0);
     }
    
     void print(pid_t pid)
     {
     printf("I get it,my pid is %d\n",pid);
     sem_getvalue(sem,&val);
     printf("Now the value have %d\n",val);
     }
    
    
    点赞 评论 复制链接分享

相关推荐