如题所述:
有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:
A:1 2 3 4 1 2....
B:2 3 4 1 2 3....
C:3 4 1 2 3 4....
D:4 1 2 3 4 1....
请设计程序。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <pthread.h> 5 #include <errno.h> 6 #include <stdarg.h> 7 #include <unistd.h> 8 #include <limits.h> 9 #include <sys/ipc.h> 10 #include <sys/msg.h> 11 #include <sys/types.h> 12 #include <sys/time.h> 13 #include <signal.h> 14 15 16 #define NUM 4 17 #define RUN 0 18 #define OVER 1 19 20 FILE *fp[NUM]; 21 int who[NUM]; 22 int shutdown; 23 pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; 24 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 25 26 27 #define myprintf( ... ) 28 do{ 29 mprintf(__FILE__, __FUNCTION__, __LINE__,##__VA_ARGS__); 30 }while(0) 31 32 33 34 35 int mprintf(const char *file_name, const char *func_name, int line, const char *fmt, ...) 36 { 37 va_list strArgPtr; 38 int la,lb; 39 la = fprintf(stdout, "[%-15s]-[%-10s]-[%04d]", file_name, func_name, line); 40 va_start(strArgPtr, fmt); 41 lb = vfprintf(stdout, fmt, strArgPtr); 42 va_end(strArgPtr); 43 printf("\n"); 44 fflush(stdout); 45 return (la+lb); 46 } 47 48 void * func(void *arg) 49 { 50 int i; 51 int name = (int)arg; 52 char tmp[50]; 53 sprintf(tmp, "%d ", name+1); 54 for(;;) 55 { 56 for( i = 0 ; i < NUM ; i++) 57 { 58 // int aim = who[i]; 59 if( pthread_mutex_lock(&mylock)) 60 { 61 myprintf("pthread_mutex_lock error errmsg[%s]\n", strerror(errno)); 62 exit(0); 63 } 64 while(1) 65 { 66 if(shutdown == OVER) 67 { 68 myprintf("thread%d exit", name); 69 pthread_exit(NULL); 70 } 71 if(who[i] == name) 72 { 73 fwrite(tmp, 2, 1, fp[i]); 74 fflush(fp[i]); 75 who[i] = (who[i]+1)%NUM; 76 break; 77 } 78 if(pthread_cond_wait(&cond, &mylock)) 79 { 80 myprintf("wait error errmsg[%s]\n", strerror(errno)); 81 exit(0); 82 } 83 } 84 if(pthread_mutex_unlock(&mylock)) 85 { 86 myprintf("pthread_mutex_unlock error errmsg[%s]\n", strerror(errno)); 87 exit(0); 88 } 89 if(pthread_cond_broadcast(&cond)) 90 { 91 myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno)); 92 exit(0); 93 } 94 } 95 } 96 } 97 98 void allover(int sig) 99 { 100 if( pthread_mutex_lock(&mylock)) 101 { 102 myprintf("pthread_mutex_lock error errmsg[%s]\n", strerror(errno)); 103 exit(0); 104 } 105 shutdown = OVER; 106 if( pthread_mutex_unlock(&mylock)) 107 { 108 myprintf("pthread_mutex_lock error errmsg[%s]\n", strerror(errno)); 109 exit(0); 110 } 111 if(pthread_cond_broadcast(&cond)) 112 { 113 myprintf("pthread_cond_broadcast error errmsg[%s]\n", strerror(errno)); 114 exit(0); 115 } 116 } 117 118 119 int appInit() 120 { 121 shutdown = RUN; 122 char filename[NUM][50]; 123 int i; 124 for( i = 0; i<NUM; i++) 125 { 126 sprintf(filename[i], "%c", ‘A‘+i); 127 fp[i] = fopen(filename[i], "w"); 128 if(fp[i] == NULL) 129 { 130 myprintf("fopen failure"); 131 exit(0); 132 } 133 who[i] = i; 134 } 135 if ( signal(SIGUSR1, allover)== SIG_ERR) 136 { 137 myprintf("signal failure"); 138 return -1; 139 } 140 // pthread_mutex_init(&mylock, NULL); 141 // pthread_cond_init(&cond, NULL); 142 return 0; 143 } 144 145 int appDone() 146 { 147 int i; 148 for( i = 0; i<NUM; i++) 149 { 150 fclose(fp[i]); 151 } 152 // pthread_cond_destroy(&cond); 153 // pthread_mutex_destroy(&mylock); 154 return 0; 155 } 156 157 int main(int argc, char *argv[]) 158 { 159 160 pthread_t th[NUM]; 161 int ret; 162 int i; 163 ret = appInit(); 164 if(ret) 165 { 166 myprintf("appinit failure"); 167 return -1; 168 } 169 for( i = 0; i < NUM; i++) 170 { 171 ret = pthread_create(&th[i], NULL, func, (void*)i); 172 if(ret) 173 { 174 myprintf("pthread_create ta error [%s]", strerror(errno)); 175 return 1; 176 } 177 } 178 179 for( i = 0; i < NUM; i++) 180 pthread_join(th[i], NULL); 181 182 myprintf("main thread exit"); 183 ret = appDone(); 184 if(ret) 185 { 186 myprintf("appDone failure"); 187 return -1; 188 } 189 return 0; 190 }
代码没有加延时,如果哪位朋友运行生成大文件,不要怪我。
时间: 2024-10-27 08:05:35