第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

作業(yè)社區(qū)

探索學(xué)習(xí)新天地,共享知識資源!

0 提交作業(yè)
0 布置作業(yè)
0 滿分作業(yè)
得分 100
學(xué)習(xí)任務(wù)

jelasin 的學(xué)生作業(yè):

#include #include #include #include #include #include #include #include #include #include #include #define ITERATIONS 10 void write_time_to_file(FILE *file) { time_t rawtime; struct tm *timeinfo; char buffer[80]; time(&rawtime); timeinfo = localtime(&rawtime); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo); fprintf(file, "%s", buffer); fflush(file); } int main() { struct sigaction sa; sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_NOCLDWAIT; sigaction(SIGCHLD, &sa, NULL); FILE *file = fopen("output.txt", "w"); if (file == NULL) { perror("fopen"); exit(EXIT_FAILURE); } sem_t *sem_parent = sem_open("/sem_parent", O_CREAT | O_EXCL, 0644, 1); sem_t *sem_child = sem_open("/sem_child", O_CREAT | O_EXCL, 0644, 0); if (sem_parent == SEM_FAILED || sem_child == SEM_FAILED) { fprintf(stderr, "sem_open failed\n"); if (errno == EEXIST) { fprintf(stderr, "Semaphore already exists\n"); if (sem_unlink("/sem_parent") < 0) { perror("sem_unlink"); exit(EXIT_FAILURE); } if (sem_unlink("/sem_child") < 0) { perror("sem_unlink"); exit(EXIT_FAILURE); } sem_t *sem_parent = sem_open("/sem_parent", O_CREAT, 0644); sem_t *sem_child = sem_open("/sem_child", O_CREAT, 0644); if (sem_trywait(sem_parent) < 0) { perror("sem_trywait"); } if (sem_trywait(sem_child) < 0) { perror("sem_trywait"); } } else { fprintf(stderr, "Error: %s\n", strerror(errno)); perror("sem_open"); sem_close(sem_parent); sem_close(sem_child); sem_unlink("/sem_parent"); sem_unlink("/sem_child"); fclose(file); exit(EXIT_FAILURE); } } pid_t pid = fork(); if (pid < 0) { perror("fork"); sem_close(sem_parent); sem_close(sem_child); sem_unlink("/sem_parent"); sem_unlink("/sem_child"); fclose(file); exit(EXIT_FAILURE); } if (pid == 0) { for (int i = 0; i < ITERATIONS; i++) { sem_wait(sem_child); sleep(1); fprintf(stderr, "Child: %d time\n", i); write_time_to_file(file); sem_post(sem_parent); } fclose(file); sem_close(sem_parent); sem_close(sem_child); exit(EXIT_SUCCESS); } else { for (int i = 1; i

得分 100
學(xué)習(xí)任務(wù)

SamstagBaron 的學(xué)生作業(yè):

#include #include #include #include #include #include #include #include #define FIFO_NAME "./fifo" int main(void) { // for(;;){ // ret = poll(&pfds,1,1000); // if (ret == -1){ // perror("[ERROR] poll(): "); // exit(EXIT_FAILURE); // }else if (ret == 0){ // printf("Timeout.\n"); // }else if (ret > 0){ // if (pfds.revents & POLLIN){ //判斷返回的就緒事件是否為 POLLIN // fgets(buffer,sizeof(buffer),stdin); // 如果是 POLLIN,則表示有用戶輸入 // printf("buffer : %s ",buffer); // } // } // } pid_t pid = fork(); if(pid==-1){ perror("[ERROR] fork() : "); exit(EXIT_FAILURE); } else if(pid==0){ if(access(FIFO_NAME,F_OK)==-1) mkfifo(FIFO_NAME,0644); char wbuffer[100]={0}; int fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK); int wbytes; while(1){ fgets(wbuffer,sizeof(wbuffer),stdin); wbytes = write(fd,wbuffer,strlen(wbuffer)); printf("write ok\n"); if(wbuffer0){ while(access(FIFO_NAME,F_OK)==-1); int fd = open(FIFO_NAME,O_RDONLY|O_NONBLOCK); int ret,maxfd = 0; struct pollfd pfds; char buffer[64] = {0}; pfds.fd = fd; pfds.events = POLLIN; maxfd = pfds.fd; while(1){ ret = poll(&pfds,1,1000); if (ret == -1){ perror("[ERROR] select(): "); exit(EXIT_FAILURE); }else if (ret == 0){ // 超時返回 printf("Timeout.\n"); }else if (ret > 0){ int rbytes; if (pfds.revents & POLLIN){ // 判斷是否在集合中 printf("buffer :"); while(1){ rbytes = read(fd,buffer,sizeof(buffer)-1); if(rbytes==0){ printf("Read Done\n"); break; } else if(rbytes>0){ buffer[rbytes]='\0'; printf(" %s",buffer); if(strncmp(buffer,"quit",4)==0){ goto END; } } else{ perror("Read Error"); break; } } } } } END: close(fd); } return 0; } 【圖片】

得分 100
學(xué)習(xí)任務(wù)

jelasin 的學(xué)生作業(yè):

#ifndef __USE_GNU #define __USE_GNU #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // system v ipc headers #include #include #include #include // posix ipc headers #include #include #include int main(int argc, char const *argv[]) { // system v ipc shared memory key_t key = ftok("/tmp", 'A'); int shm_id_sysv = shmget(key, 1024, IPC_CREAT | 0666); if (shm_id_sysv == -1) { perror("shmget"); exit(1); } void *shm_ptr_sysv = shmat(shm_id_sysv, NULL, 0); if (shm_ptr_sysv == NULL) { perror("shmat"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_id_sysv); FILE *fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } char line[1024]; void *ptr_shm = shm_ptr_sysv; ssize_t nbytes = 0; while ((nbytes = fread(line, 1, 1024, fp)) > 0) { strncpy(ptr_shm, line, nbytes); ptr_shm = (int8_t*)ptr_shm + nbytes; } pclose(fp); strcpy(ptr_shm, "Hello, system v ipc shared memory!\n"); pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // child process char buf[1024]; key_t key = ftok("/tmp", 'A'); int shm_id = shmget(key, 1024, 0); void *shm_ptr = shmat(shm_id, NULL, 0); strcpy(buf, shm_ptr); fprintf(stderr, "Child process: %d\n", getpid()); fprintf(stderr, "Read from Shared memory: %s\n", buf); exit(EXIT_SUCCESS); } else { // parent process int status; waitpid(pid, &status, 0); } if (shmctl(shm_id_sysv, IPC_RMID, NULL) == -1) { perror("shmctl"); return -1; } // -------------------------------------------------------------------------------------- // posix ipc shared memory int shm_fd_posix = shm_open("/posix_shm", O_CREAT | O_RDWR, 0666); if (shm_fd_posix == -1) { perror("shm_open"); exit(1); } if (ftruncate(shm_fd_posix, 1024) == -1) { perror("ftruncate"); exit(1); } void *shm_ptr_posix = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_posix, 0); if (shm_ptr_posix == MAP_FAILED) { perror("mmap"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_fd_posix); fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } ptr_shm = shm_ptr_posix; while ((nbytes = fread(line, 1, 1024, fp)) > 0) { strncpy(ptr_shm, line, nbytes); ptr_shm = (int8_t*)ptr_shm + nbytes; } pclose(fp); strcpy(ptr_shm, "Hello, posix ipc shared memory!\n"); pid = fork(); if (pid == -1) { perror("fork"); exit(1); } else if (pid == 0) { // child process int shm_fd = shm_open("/posix_shm", O_RDONLY, 0666); void *shm_ptr = mmap(NULL, 1024, PROT_READ, MAP_SHARED, shm_fd, 0); char buf[1024]; strcpy(buf, shm_ptr); fprintf(stderr, "Child process: %d\n", getpid()); fprintf(stderr, "Read from Shared memory: %s\n", buf); exit(EXIT_SUCCESS); } else { // parent process int status; waitpid(pid, &status, 0); } if (munmap(shm_ptr_posix, 1024) == -1) { perror("munmap"); exit(1); } if (close(shm_fd_posix) == -1) { perror("close"); exit(1); } if (shm_unlink("/posix_shm") == -1) { perror("shm_unlink"); exit(1); } return 0; } ? 5 ./main Shared memory: 15 Child process: 18101 Read from Shared memory: ------------ 共享內(nèi)存段 -------------- 鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài) 0x00000000 3 jelasin 606 10483788 2 目標(biāo) 0x00000000 4 jelasin 606 10483788 2 目標(biāo) 0x41030001 15 jelasin 666 1024 1 Hello, system v ipc shared memory! Shared memory: 3 Child process: 18104 Read from Shared memory: ------------ 共享內(nèi)存段 -------------- 鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài) 0x00000000 3 jelasin 606 10483788 2 目標(biāo) 0x00000000 4 jelasin 606 10483788 2 目標(biāo) 0x00000000 15 jelasin 666 1024 1 目標(biāo) Hello, posix ipc shared memory!

得分 100
學(xué)習(xí)任務(wù)

jelasin 的學(xué)生作業(yè):

#ifndef __USE_GNU #define __USE_GNU #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // system v ipc headers #include #include #include #include // posix ipc headers #include #include #include int main(int argc, char const *argv[]) { // system v ipc shared memory key_t key = ftok("/tmp", 'A'); int shmid = shmget(key, 1024, IPC_CREAT | 0666); if (shmid == -1) { perror("shmget"); exit(1); } void *shm = shmat(shmid, NULL, 0); if (shm == NULL) { perror("shmat"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shmid); FILE *fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } char line[1024]; while (fgets(line, sizeof(line), fp)) { fprintf(stderr, "%s", line); } pclose(fp); if (shmctl(shmid, IPC_RMID, NULL) == -1) { perror("shmctl"); return -1; } // posix ipc shared memory int shm_fd = shm_open("/posix_shm", O_CREAT | O_RDWR, 0666); if (shm_fd == -1) { perror("shm_open"); exit(1); } if (ftruncate(shm_fd, 1024) == -1) { perror("ftruncate"); exit(1); } void *shm_ptr = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); if (shm_ptr == MAP_FAILED) { perror("mmap"); exit(1); } fprintf(stderr, "Shared memory: %d\n", shm_fd); fp = popen("ipcs -m", "r"); if (fp == NULL) { perror("popen"); exit(1); } while (fgets(line, sizeof(line), fp)) { fprintf(stderr, "%s", line); } pclose(fp); if (munmap(shm_ptr, 1024) == -1) { perror("munmap"); exit(1); } if (close(shm_fd) == -1) { perror("close"); exit(1); } if (shm_unlink("/posix_shm") == -1) { perror("shm_unlink"); exit(1); } return 0; } ? 5 clang main.c -o main -lpthread ? 5 ./main Shared memory: 6 ------------ 共享內(nèi)存段 -------------- 鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài) 0x00000000 3 jelasin 606 10483788 2 目標(biāo) 0x00000000 4 jelasin 606 10483788 2 目標(biāo) 0x41030001 6 jelasin 666 1024 1 Shared memory: 3 ------------ 共享內(nèi)存段 -------------- 鍵 shmid 擁有者 權(quán)限 字節(jié) 連接數(shù) 狀態(tài) 0x00000000 3 jelasin 606 10483788 2 目標(biāo) 0x00000000 4 jelasin 606 10483788 2 目標(biāo) 0x00000000 6 jelasin 666 1024 1 目標(biāo)

得分 100
學(xué)習(xí)任務(wù)

向佐佐 的學(xué)生作業(yè):

#include #include #include #include #include #include static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int number = 0; // 記錄倉庫里的產(chǎn)品的數(shù)量 static bool done = false; // 標(biāo)志位,表示生產(chǎn)是否完成 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 生產(chǎn)者線程的執(zhí)行函數(shù) void* thread_handler(void* arg) { int cnt = atoi((char*)arg); for (int i = 0; i < cnt; i++) { pthread_mutex_lock(&mutex); printf("線程[%ld]生產(chǎn)一個產(chǎn)品,產(chǎn)品數(shù)量為:%d\n", pthread_self(), ++number); pthread_mutex_unlock(&mutex); pthread_cond_broadcast(&cond); // 喚醒所有在cond阻塞的消費(fèi)者線程 } pthread_exit(NULL); return NULL; } // 消費(fèi)者線程執(zhí)行函數(shù) void* thread_handler_consume(void* arg) { while (true) { pthread_mutex_lock(&mutex); while (number == 0 && !done) { // 倉庫中沒有產(chǎn)品且生產(chǎn)未完成,則等待 pthread_cond_wait(&cond, &mutex); } if (done && number == 0) { // 生產(chǎn)完成且倉庫為空,退出 pthread_mutex_unlock(&mutex); break; } // 消費(fèi)產(chǎn)品 //total_of_consume++; printf("線程[%ld]消費(fèi)一個產(chǎn)品,產(chǎn)品數(shù)量:%d\n", pthread_self(), --number); sleep(1); // 模擬消費(fèi)時間 pthread_mutex_unlock(&mutex); } pthread_exit(NULL); return NULL; } // main函數(shù)輸入argc個參數(shù) // argv[1]~argv[argc-2]代表創(chuàng)建argc-2個生產(chǎn)者線程,每個線程分別生產(chǎn)1,2,3,...,argc-2個產(chǎn)品 // argv[argc-1]代表創(chuàng)建argv[argc-1]個消費(fèi)者線程 int main(int argc, char* argv[]) { pthread_t tid_p[argc - 2]; pthread_t tid_c[atoi(argv[argc - 1])]; int i, err; // 創(chuàng)建生產(chǎn)者線程 for (i = 1; i < argc - 1; i++) { err = pthread_create(&tid_p[i - 1], NULL, thread_handler, (void*)argv[i]); if (err != 0) { fprintf(stderr, "[ERROR]pthread_product_create():\n", strerror(err)); exit(EXIT_FAILURE); } } // 創(chuàng)建消費(fèi)者線程 for (i = 0; i < atoi(argv[argc - 1]); i++) { err = pthread_create(&tid_c[i], NULL, thread_handler_consume, NULL); if (err != 0) { fprintf(stderr, "[ERROR]pthread_consume_create():\n", strerror(err)); exit(EXIT_FAILURE); } } // 等待所有生產(chǎn)者線程完成 for (i = 0; i < argc - 2; i++) { pthread_join(tid_p[i], NULL); } // 設(shè)置生產(chǎn)完成標(biāo)志 done = true; // 等待所有消費(fèi)者線程完成 for (i = 0; i < atoi(argv[argc - 1]); i++) { pthread_join(tid_c[i], NULL); } printf("所有產(chǎn)品已消費(fèi)完畢,程序結(jié)束。\n"); return 0; } 【圖片】

得分 100
學(xué)習(xí)任務(wù)

SamstagBaron 的學(xué)生作業(yè):

#include #include #include #include #include #include #include #include #define FIFO_NAME "./fifo" int main(void) { int ret; int maxfd = 1; fd_set readfds,tmpfds; struct timeval tv = {1,0},tmp_tv; char buffer[64] = {0}; pid_t pid = fork(); if(pid==-1){ perror("[ERROR] fork() : "); exit(EXIT_FAILURE); } else if(pid==0){ if(access(FIFO_NAME,F_OK)==-1) mkfifo(FIFO_NAME,0644); char wbuffer[100]={0}; int fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK); int wbytes; while(1){ fgets(wbuffer,sizeof(wbuffer),stdin); wbytes = write(fd,wbuffer,strlen(wbuffer)); printf("write ok\n"); if(wbuffer0){ while(access(FIFO_NAME,F_OK)==-1); int fd = open(FIFO_NAME,O_RDONLY|O_NONBLOCK); FD_ZERO(&readfds); FD_SET(fd,&readfds); while(1){ tmp_tv = tv; tmpfds = readfds; ret = select(fd + 1,&tmpfds,NULL,NULL,&tmp_tv); if (ret == -1){ perror("[ERROR] select(): "); exit(EXIT_FAILURE); }else if (ret == 0){ // 超時返回 printf("Timeout.\n"); }else if (ret > 0){ int rbytes; if (FD_ISSET(fd,&tmpfds)){ // 判斷是否在集合中 printf("buffer :"); while(1){ rbytes = read(fd,buffer,sizeof(buffer)-1); if(rbytes==0){ printf("Read Done\n"); break; } else if(rbytes>0){ buffer[rbytes]='\0'; printf(" %s",buffer); if(strncmp(buffer,"quit",4)==0){ goto END; } } else{ perror("Read Error"); break; } } } } } END: close(fd); } return 0; } 【圖片】

微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號