为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

别摸我的键盘 的学生作业:

有互斥锁 #include #include #include #include static int global = 0; //pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mutex; void *do_thread(void *arg) { int loops = *(int *)arg; int i,tmp = 0; for (i = 0;i < loops;i++){ pthread_mutex_lock(&mutex); tmp = global; tmp++; global = tmp; pthread_mutex_unlock(&mutex); } pthread_exit(NULL); } int main(int argc,char *argv[]) { int err,i = 0; pthread_t tid[2] = {0}; int loops = 0; if (argc != 2){ fprintf(stderr,"Usage : < %s > < count loops>\n",argv[0]); exit(EXIT_FAILURE); } loops = atoi(argv[1]); pthread_mutex_init(&mutex, NULL); for (i = 0;i < 2;i++){ err = pthread_create(&tid[i],NULL,do_thread,&loops); if (err != 0){ fprintf(stderr,"[ERROR] pthread_create(): < %s > \n",strerror(err)); exit(EXIT_FAILURE); } } pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); pthread_mutex_destroy(&mutex); printf("global = %d\n",global); return 0; } 执行结果 linux@linux:~/test/10-11/1-2$ ./a.out 10000 global = 20000 linux@linux:~/test/10-11/1-2$ ./a.out 100000 global = 200000 linux@linux:~/test/10-11/1-2$ ./a.out 1000000 global = 2000000 linux@linux:~/test/10-11/1-2$ ./a.out 10000000 global = 20000000 无互斥锁代码 #include #include #include #include static int global = 0; void *do_thread(void *arg) { int loops = *(int *)arg; int i,tmp = 0; for (i = 0;i < loops;i++){ tmp = global; tmp++; global = tmp; } pthread_exit(NULL); } int main(int argc,char *argv[]) { int err,i = 0; pthread_t tid[2] = {0}; int loops = 0; if (argc != 2){ fprintf(stderr,"Usage : < %s > < count loops>\n",argv[0]); exit(EXIT_FAILURE); } loops = atoi(argv[1]); for (i = 0;i < 2;i++){ err = pthread_create(&tid[i],NULL,do_thread,&loops); if (err != 0){ fprintf(stderr,"[ERROR] pthread_create(): < %s > \n",strerror(err)); exit(EXIT_FAILURE); } } pthread_join(tid[0],NULL); pthread_join(tid[1],NULL); printf("global = %d\n",global); return 0; } 无互斥锁执行结果 linux@linux:~/test/10-11/1-2$ ./a.out 100 global = 200 linux@linux:~/test/10-11/1-2$ ./a.out 200 global = 400 linux@linux:~/test/10-11/1-2$ ./a.out 10000 global = 20000 linux@linux:~/test/10-11/1-2$ ./a.out 100000 global = 123754 linux@linux:~/test/10-11/1-2$ ./a.out 1000000 global = 1003933

得分 100
讨论题

jelasin 的学生作业:

//seqstack.h #ifndef __SEQSTACK_H__ #define __SEQSTACK_H__ #include #include #include #include #include typedef int data_t; #define MAX 20 typedef struct { data_t buf[MAX]; int top; } seqstack_t; extern seqstack_t* create_empty_seqstack(); extern bool is_empty_seqstack(seqstack_t* s); extern bool is_full_seqstack(seqstack_t* s); extern void push_seqstack(seqstack_t*s,data_t data); extern data_t pop_seqstack(seqstack_t* s); extern data_t get_top_data(seqstack_t *s); #endif //seqstack.c #include "seqstack.h" seqstack_t* create_empty_seqstack() { seqstack_t* stack = (seqstack_t*)malloc(sizeof(seqstack_t)); if (NULL == stack) { perror("Memory allocation failed"); return NULL; } stack->top = -1; return stack; } bool is_empty_seqstack(seqstack_t* s) { return s->top == -1; } bool is_full_seqstack(seqstack_t* s) { return s->top == MAX - 1; } void push_seqstack(seqstack_t*s,data_t data) { if (is_full_seqstack(s)) { perror("Stack overflow"); return; } s->buf[++s->top] = data; } data_t pop_seqstack(seqstack_t* s) { if (is_empty_seqstack(s)) { perror("Stack underflow"); return -1; } return s->buf[s->top--]; } data_t get_top_data(seqstack_t *s) { if (is_empty_seqstack(s)) { perror("Stack is empty"); return -1; } return s->buf[s->top]; } //main.c #include "seqstack.h" int main(int argc, char const *argv[]) { data_t data[] = {'a','n','i','h','c',' ','e','v','o','l',' ','I'}; seqstack_t* s = create_empty_seqstack(); for (size_t i = 0; i I ==> ==> l ==> o ==> v ==> e ==> ==> c ==> h ==> i ==> n ==> a

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号