/************************************************************ * function: clear the overdue items from the queue. * sf: a pointer to function which can calculate the critical index * return: 0, correct; others, error number. ***********************************************************/ int shmmq_clear(SF sf) { int ret = 0; int begin; int end; if (sf == NULL) { return ERROR_INVALID_ARGUMENT; } if (-1 == semaphore_p(g_s_semid)) { return ERROR_SEMAPHORE_P; } begin = g_s_phead->deq_pos % g_s_mqsize; end = g_s_phead->enq_pos % g_s_mqsize; begin = (*sf)(begin, end); if (begin < 0 || begin >= g_s_mqsize) { ret = ERROR_INVALID_ARGUMENT; } if (-1 == semaphore_v(g_s_semid)) { return ERROR_SEMAPHORE_V; } return ret; }
/************************************************************ * function: dequeueing. * item: a pointer to the element to dequeue * size: the size of the element * return: 0, correct; others, error number. ***********************************************************/ int shmmq_dequeue(void* item, int size) { int ret = 0; if (!item || size <= 0 || size > g_s_itemsize) { return ERROR_INVALID_ARGUMENT; } if (-1 == semaphore_p(g_s_semid)) { return ERROR_SEMAPHORE_P; } if (g_s_phead->enq_pos == g_s_phead->deq_pos) { ret = ERROR_MSGQUEUE_FULL; goto queue_full; } unsigned int pos = (g_s_phead->deq_pos+1) % g_s_mqsize; memcpy(item, g_s_pcontent + pos * g_s_itemsize, size); g_s_phead->deq_pos++; queue_full: if (-1 == semaphore_v(g_s_semid)) { return ERROR_SEMAPHORE_V; } return ret; }
int main(int argc,char **argv) { int i ; int pause_time; char op_char = 'O'; srand((unsigned int)getpid()); //create semaphore sem_id = semget((key_t)1234,1,0666|IPC_CREAT); if (argc >1) { //set sempvalue 初始化信号量 if (!set_semvalue()) { fprintf(stderr,"Failed to initialize semaphore"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(2); } for (i = 0; i < 10; ++i) { //设置信号量 等待进入 if (!semaphore_p()) { exit(EXIT_FAILURE); } printf("%c",op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c",op_char); fflush(stdout); //退出临界区,信号量设置为可用 if (!semaphore_v()) { exit(EXIT_FAILURE); } pause_time = rand() % 3; sleep(pause_time); } printf("\n%d - finished \n",getpid()); if (argc>1) { sleep(10); //删除信号量 del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char message = 'X'; int i = 0; /* 创建信号量 */ sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); if(argc > 1) { /* 程序第一次被调用,初始化信号量 */ if(!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } /* 设置要输出到屏幕中的信息,即其参数的第一个字符 */ message = argv[1][0]; sleep(2); } for(i = 0; i < 10; ++i) { /* 进入临界区 */ if(!semaphore_p()) { exit(EXIT_FAILURE); } /* 向屏幕中输出数据 */ printf("%c", message); /* 清理缓冲区,然后休眠随机时间 */ fflush(stdout); sleep(rand() % 3); /* 离开临界区前再一次向屏幕输出数据 */ printf("%c", message); fflush(stdout); /* 离开临界区,休眠随机时间后继续循环 */ if(!semaphore_v()) { exit(EXIT_FAILURE); } sleep(rand() % 2); } sleep(10); printf("\n%d - finished\n", getpid()); if(argc > 1) { /* 如果程序是第一次被调用,则在退出前删除信号量 */ sleep(3); del_semvalue(); } exit(EXIT_SUCCESS); }
int main () { int i; int pause_time; int status; char op_char = 'O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); sleep(2); //if (!set_semvalue()) //{ // fprintf(stderr, "Failed to initialize semaphore\n"); // exit(EXIT_FAILURE); //} //op_char = 'X'; //sleep(2); char *argc[] = {"./printer","Process_Read",NULL}; for(i = 0; i < 10; i++) { if (!semaphore_p()) exit(EXIT_FAILURE); pid_t pid = fork(); if(pid == (pid_t)0) { execv("./printer",argc); } //fflush(stdout); //pause_time = rand() % 3; sleep(1); //printf("%c", op_char); //fflush(stdout); pid_t pid_second = fork(); if(pid_second == (pid_t)0) { execv("./printer",argc); } int ret_child = waitpid(pid,&status,0); int ret_s_child = waitpid(pid_second,&status,0); printf("Getting Second Here \n"); if (!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand() % 2; sleep(pause_time); } printf("\n%d - finished\n", getpid()); //if (argc > 1) //{ // sleep(4); //del_semvalue(); //} exit(EXIT_SUCCESS); }
int main(int argc, char* argv[]) { int i; int pause_time; char op_char = 'O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(2); } /* Then we have a loop which enters and leaves the critical section ten times. There, we first make a call to semaphore_p which sets the semaphore to wait, as this program is about to enter the critical section. */ for (i = 0; i < 10; i++) { if (!semaphore_p()) { exit(EXIT_FAILURE); } printf("%c", op_char); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char); fflush(stdout); /* After the critical section, we call semaphore_v, setting the semaphore available, before going through the for loop again after a random wait. After the loop, the call to del_semvalue is made to clean up the code. */ if (!semaphore_v()) { exit(EXIT_FAILURE); } pause_time = rand() % 2; sleep(pause_time); } printf("\n%d - finished\n", getpid()); if (argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char message = 'X'; int i = 0; key_t key; key = ftok("/var/httcsec",IPC_ID); //创建信号量 printf("key:%d\n", (int)key); sem_id = semget(key, 1, 0666 | IPC_CREAT); if(argc > 1) { //程序第一次被调用,初始化信号量 if(!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } //设置要输出到屏幕中的信息,即其参数的第一个字符 message = argv[1][0]; sleep(2); } for(i = 0; i < 3; ++i) { //进入临界区 if(!semaphore_p()) exit(EXIT_FAILURE); //向屏幕中输出数据 printf("..[%d]..lock\n", getpid()); //清理缓冲区,然后休眠随机时间 fflush(stdout); sleep(rand() % 5); //离开临界区前再一次向屏幕输出数据 printf("..[%d]..unlock\n", getpid()); fflush(stdout); //离开临界区,休眠随机时间后继续循环 if(!semaphore_v()) exit(EXIT_FAILURE); printf("..[%d]..waiting\n", getpid()); fflush(stdout); } printf("\n%d - finished\n", getpid()); if(argc > 1) { //如果程序是第一次被调用,则在退出前删除信号量 sleep(3); del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i; srand((unsigned int)getpid()); sem_id = semget((key_t)4321, 1, 0666); printf("sem_id_%s = %d\n",argv[1], sem_id); if(sem_id < 0) { sem_id = semget((key_t)4321, 1, 0666|IPC_CREAT); printf("sem_id_2_%s = %d\n", argv[1] , sem_id); } if(argc > 1) { if(!set_semvalue()) { fprintf(stderr, "Failed to initlize semaphore\n"); exit(EXIT_FAILURE); } op_char = argv[1]; // sleep(2); } for(i = 0; i< 10; i++) { /****************** P *********************/ if(!semaphore_p()) exit(EXIT_FAILURE); do_my_job(); //do something /****************** V *********************/ if(!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand()%2; // sleep(pause_time); }//for printf("\n%d - finished \n", getpid()); if(argc > 1) { // sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i; int pause_time; char op_char='O'; srand((unsigned int)getpid()); sem_id = semget((key_t)4321, 1, 0666|IPC_CREAT); if(argc > 1) { if(!set_semvalue()) { fprintf(stderr, "Failed to initlize semaphore\n"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(2); } for(i = 0; i< 10; i++) { if(!semaphore_p()) exit(EXIT_FAILURE); printf("%c is doing job\n", op_char); fflush(stdout); pause_time = rand()%3; sleep(pause_time); printf("%c is done\n", op_char); fflush(stdout); if(!semaphore_v()) exit(EXIT_FAILURE); pause_time = rand()%2; sleep(pause_time); }//for printf("\n%d - finished \n", getpid()); if(argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i; int pause_time; char op_char = 'O'; // initialize the random seed srand( (unsigned int) getpid() ); // creates a new semaphore (only 1) with modes + create if doesn't ex sem_id = semget( (key_t)1234, 1, 0666 | IPC_CREAT); // if invoked with parameters (must be the first invocation) if ( argc > 1 ) { if ( ! set_semvalue() ) { fprintf (stderr, "failed to init semaphore\n"); exit (EXIT_FAILURE); } op_char = 'X'; // gives time to start the second semaphore sleep (2); } // normal run for ( i = 0 ; i < 10 ; i++ ) { if ( !semaphore_p() ) exit (EXIT_FAILURE); printf ("%c", op_char); fflush (stdout); pause_time = rand() % 3; sleep (pause_time); printf("%c", op_char); fflush (stdout); if ( ! semaphore_v() ) exit (EXIT_FAILURE); pause_time = rand() % 2; sleep (pause_time); } printf ("\n%d - finished\n", getpid() ); if (argc > 1 ) { sleep (10); del_semvalue(); } exit (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int sem_id; char ch = 'O'; int i, pause_time; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666|IPC_CREAT); if(sem_id < 0) { fprintf(stderr, "get semaphore id failed\n"); exit(1); } if(argc > 1) { if(!set_semvalue(sem_id)) { fprintf(stderr, "initialize semaphore failed\n"); exit(1); } ch = 'X'; sleep(2); } for(i = 0; i < 10; ++i) { if(!semaphore_p(sem_id)) exit(1); printf("%c", ch); fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", ch); fflush(stdout); if(!semaphore_v(sem_id)) exit(1); } if(argc > 1) { sleep(10); del_semvalue(sem_id); } exit(0); }
int main(int argc, char *argv[]) { int i; int pause_time; char op_char = 'O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); // if there's an arg, set op_char to X and create the semaphore if (argc > 1) { if (!set_semvalue()) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } op_char = 'X'; sleep(2); } // take turns locking, printing to stdout and flushing, // and then unlocking and waiting. for(i = 0; i < 10; i++) { // lock around the printing and a short pause if (!semaphore_p()) exit(EXIT_FAILURE); printf("%c", op_char);fflush(stdout); pause_time = rand() % 3; sleep(pause_time); printf("%c", op_char);fflush(stdout); if (!semaphore_v()) exit(EXIT_FAILURE); // then do a random pause while unlocked pause_time = rand() % 2; sleep(pause_time); } printf("\n%d - finished\n", getpid()); // it's very important to delete the semaphore you created! if (argc > 1) { sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
int main(int argc,char *argv[]){ int i; int pause_time; char op_char='O'; srand((unsigned int)getpid()); sem_id = semget((key_t)1234,1,0666|IPC_CREAT);// 產生一個新號誌 或者根據key 取得一個已存在的號誌 666為設定權限 if(argc >1){ // 假設第一次執行時 使用參數 if(!set_semvalue()){ // setctl回傳-1時(發生錯誤) fprintf(stderr,"Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } op_char='X'; sleep(2); } /* 然後進入迴圈 進出入 "critical section" 十次 * 在那裡 首先先呼叫semaphore_p 讓即將進入critical section 的semaphore等待*/ for(i=0;i<10;i++){ if(!semaphore_p()) exit(EXIT_FAILURE); printf("%c",op_char);fflush(stdout); pause_time=rand()%3; sleep(pause_time); printf("%c",op_char);fflush(stdout); /* 在critical section 之後 呼叫semaphore_v * 再進去for loop 之前 等待一段亂數時間 然後del sem 結束*/ if(!semaphore_v()) exit(EXIT_FAILURE); pause_time=rand()%2; sleep(pause_time); } printf("\n%d - finished\n",getpid()); if(argc>1){ sleep(10); del_semvalue(); } exit(EXIT_SUCCESS); }
int main() { int fd1,fd2,file1,recived,sem_id; datavar.arry='*'; datavar.arr[0]=20; datavar.arr[1]=20; semaphore_p(); // recived=semop(sem_id,struct sembuf *sem,SEM_UNDO); // printf("semid is %d\n",sem_id); // sem.sem_num=0; // sem.sem_ops=-1; fd1=open("fifo_server",O_WRONLY); printf("process pid is %d\n",getpid()); recived=write(fd1,&datavar,sizeof(datavar)); close(fd1); // printf("number of written by are %d\n",recived); // printf("writen data is %c %d %d\n",datavar.arry,datavar.arr[0],datavar.arr[1]); if(access("fifo_client1",F_OK) == -1) file1=mkfifo("fifo_client1",0777); if(file1<0) { printf("file not created successfuly\n"); } else { printf("file created successfully and pid is %d\n",getpid()); } fd2=open("fifo_client1",O_RDONLY); printf("file discriptor is %d\n",fd2); recived=read(fd2,&datavar1,sizeof(datavar)); close(fd2); //printf("number of bytes read are %d\n",recived); printf("result of sum (20*20)= %d\n",datavar1.result); semaphore_v(); // printf("end of program\n"); //sem.sem_ops=1; //semop(sem_id,struct sembuf *sem_ops,SEM_UNDO); }
int main() { signal (SIGTERM, out); signal (SIGINT, out); int shmid; /* искать сегмент, соответствующий ключу key*/ shmid = shmget((key_t)1234, sizeof(struct memFormat), 0666); if (shmid == -1) {perror("shmget");return 0;} shared_memory = shmat(shmid, (void *)0, SHM_RDONLY); if (shared_memory == (void *)-1) {perror("shmat");return 0;} int sem_id = createSem(1,12345); if(sem_id==-1) {perror("createSem"); return 0;} if(set_semvalue(sem_id,0)==-1) {perror("set_semvalue"); return 0;} semaphore_p(sem_id); memcpy(&memf,shared_memory, sizeof(struct memFormat)); semaphore_v(sem_id); if (shmdt(shared_memory) == -1) {perror("shmat"); return 0;} int size = sizeof(struct memFormat)+(memf.max*memf.sizeItem); printf("Moniotr attache mem size: %d + %d=%d",sizeof(struct memFormat),(memf.max*memf.sizeItem),size); shmid = shmget((key_t)1234, size, 0666); if (shmid == -1) {perror("shmget2");return 0;} shared_memory = shmat(shmid, (void *)0, SHM_RDONLY); if (shared_memory == (void *)-1) {perror("shmat2");return 0;} pitem = (int*)shared_memory+sizeof(struct memFormat); printf("Monitor memory attached at %p,data attached at %p\n", shared_memory,pitem); printf("memory pid %d:\n sizeItem %d, count %d, max %d",memf.pidMaster,memf.sizeItem,memf.count,memf.max); sleep(1); while(bOut==false) { pitem = (int*)shared_memory+sizeof(struct memFormat); delay(500); int count1=0; if(!semaphore_p(sem_id)) {perror("semaphore_p");break;} memcpy(&memf,shared_memory, sizeof(struct memFormat)); for(int vi=0;vi<memf.count;vi++) { if(*pitem==1) count1++; //printf("%d.",*pitem); pitem++; } if(!semaphore_v(sem_id)) {perror("semaphore_v");break;} printf("\nnumber '1': %d.",count1); } if (shmdt(shared_memory) == -1) {perror("shmat2");} del_semvalue(sem_id,0); printf("\nExit...\n"); }
int main() { //counters and size variables int nread; int n = 0; //buffer to read in from the file char buffer[BUFSIZ]; //declaring shared memory space and message void *shared_memory = (void *)0; struct buffer_msg *msg; int id; //declaring the necessary semaphore ids int sem_id_s; int sem_id_n; int sem_id_e; //creating the semaphores and setting their values sem_id_n = semget((key_t)key_n, 1, 0666 | IPC_CREAT); sem_id_s = semget((key_t)key_s, 1, 0666 | IPC_CREAT); sem_id_e = semget((key_t)key_e, 1, 0666 | IPC_CREAT); if (!set_semvalue(1, sem_id_s)) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } if (!set_semvalue(0, sem_id_n)) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } if (!set_semvalue(NBUFFER, sem_id_e)) { fprintf(stderr, "Failed to initialize semaphore\n"); exit(EXIT_FAILURE); } //creating and attaching the shared memory with its address space id = shmget((key_t)key, MEMSIZE, 0666|IPC_CREAT); if(id==-1) { fprintf(stderr, "shmget failed\n"); exit(EXIT_FAILURE); } shared_memory = shmat(id, (void *)0, 0); if(shared_memory == (void *)-1) { fprintf(stderr, "shmat failed\n"); exit(EXIT_FAILURE); } //reading in the message from the file nread = read(0, buffer, BUFSIZ); //BUFSIZ is from stdio.h if(nread==-1) { fprintf(stderr, "nread failed\n"); exit(EXIT_FAILURE); } //iterate through the read in message int i = 0; int count = 0; msg = (struct buffer_msg *)shared_memory; while(n<nread) { //wait to be able to add something into the shared memory buffer (so it does not overflow) and ensure mutual exclusion is held semaphore_p(sem_id_e); semaphore_p(sem_id_s); //these can be commented out to achieve the same result if there is only one producer and one consumer //copy what is in the read in buffer to shared memory strncpy(msg->buffer[i].msg, buffer+n, TEXTSIZE); //put the size of the message in shared memory msg->buffer[i].size = strlen(msg->buffer[i].msg); count+=msg->buffer[i].size; //signal that something is in the shared memory buffer and that it has left the critical section semaphore_v(sem_id_s); //these can be commented out to achieve the same result if there is only one producer and one consumer semaphore_v(sem_id_n); i = (i + 1) % NBUFFER; n += TEXTSIZE; } printf("Read in %i bytes\n", count); }
/************************************************************ * function: create or open the shared memory. * iscreated: 1, to create the shm; 0, only to open the shm. * mqsize: the count of the items * itemsize: the size of an item (bytes) * return: 0, correct; others, error number. ***********************************************************/ int shmmq_open(int iscreated, int mqsize, int itemsize) { int nflag; int ret = 0; /* Initial semaphore */ g_s_semid = get_semvalue((key_t)SEM_KEY); if (-1 == g_s_semid) { return ERROR_GET_SEMVALUE; } if (iscreated && -1 == set_semvalue(g_s_semid)) { return ERROR_SET_SEMVALUE; } if (-1 == semaphore_p(g_s_semid)) { return ERROR_SEMAPHORE_P; } while (1) { if (iscreated) { nflag = 0666 | IPC_CREAT; } else { nflag = 0666; } g_s_mqsize = (mqsize <= 0) ? DEFAULT_MQ_SIZE : mqsize; g_s_itemsize = (itemsize <= 0) ? DEFAULT_ITEM_SIZE : itemsize; /* Initial shared memory */ g_s_shmid = shmget((key_t)SHM_KEY, g_s_mqsize * g_s_itemsize + DEFAULT_HEAD_SIZE, nflag); if (-1 == g_s_shmid) { ret = ERROR_SHMGET; break; } g_s_pbase = shmat(g_s_shmid, NULL, 0); if ((void *)-1 == g_s_pbase) { g_s_pbase = NULL; ret = ERROR_SHMAT; break; } g_s_phead = (shmmq_head*)g_s_pbase; g_s_pcontent = g_s_pbase + DEFAULT_HEAD_SIZE; /* Initial head of the shared memory */ if (iscreated) { g_s_phead->enq_pos = -1; g_s_phead->deq_pos = -1; } break; } if (ret && iscreated) { if (g_s_shmid >= 0) shmctl(g_s_shmid, IPC_RMID, 0); semaphore_v(g_s_semid); del_semvalue(g_s_semid); return ret; } if (-1 == semaphore_v(g_s_semid)) { return ERROR_SEMAPHORE_V; } return ret; }
int main() { int ret,result,fd; req cli; union semun sem_union; signal(SIGUSR1,sig_hand); cli.opr = '-'; cli.opr1= 10; cli.opr2= 5; cli.pid = getpid(); if(make_fifo("Wfifo") == -1){ fprintf(stderr,"Wfifo fails\n"); exit(EXIT_FAILURE); } if(make_fifo("Rfifo") == -1){ fprintf(stderr,"Rfifo fails\n"); exit(EXIT_FAILURE); } sem_id = semget((key_t)123,1,0666|IPC_CREAT); sem_union.val = 1; if(semctl(sem_id,0,SETVAL,sem_union) == -1) { printf("ERROR: semctl fails\n"); exit(EXIT_FAILURE); } semaphore_p(); fd = open("Wfifo",O_WRONLY); if(fd == -1) { printf("ERROR: Wfifo open fails\n"); exit(EXIT_FAILURE); } ret = write(fd,&cli,sizeof(cli)); printf("%d bytes write %d: %d\n",getpid(),fd,ret); close(fd); semaphore_v(); //semaphore_p(); pause(); fd = open("Rfifo",O_RDONLY); if(fd == -1) { printf("ERROR: Rfifo open fails\n"); exit(EXIT_FAILURE); } ret = read(fd,&cli,sizeof(cli)); printf("%d bytes read: %d & GOT: %d %c %d = %d\n",getpid(),ret,cli.opr1,cli.opr,cli.opr2,cli.result); close(fd); //semaphore_v(); /* if(semctl(sem_id,0,IPC_RMID,sem_union) == -1) { printf("ERROR: semctl fails\n"); exit(EXIT_FAILURE); } */ //unlink("Wfifo"); return 0; }
static int basic_test(void) { pid_t pid; int fd, sem_id, status, ret = 0; unsigned long i, j, chunk_no = 0, num_chunks = 0; struct write_unit wu; sem_id = semaphore_init(1); if (sem_id < 0) return sem_id; num_chunks = file_size / CHUNK_SIZE; open_rw_flags |= O_DIRECT; open_ro_flags |= O_DIRECT; if (test_flags & BASC_TEST) { fprintf(stdout, "# Prepare file in %lu length.\n", file_size); ret = prep_orig_file_in_chunks(workfile, file_size); if (ret) return ret; } fflush(stderr); fflush(stdout); signal(SIGCHLD, sigchld_handler); fd = open_file(workfile, open_rw_flags); if (fd < 0) return fd; if (test_flags & FIHL_TEST) { fprintf(stdout, "# Reserve a hole by truncating file to %lu.\n", file_size); ret = ftruncate(fd, file_size); if (ret) { ret = errno; fprintf(stderr, "ftruncate faile:%d,%s\n", ret, strerror(ret)); return ret; } } fprintf(stdout, "# Fork %lu processes performing writes.\n", num_children); for (i = 0; i < num_children; i++) { pid = fork(); if (pid < 0) { fprintf(stderr, "Fork process error!\n"); return pid; } if (pid == 0) { srand(getpid()); for (j = 0; j < num_chunks; j++) { if (verbose) fprintf(stdout, " #%d process writes " "#%lu chunk\n", getpid(), chunk_no); if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } if (test_flags & APPD_TEST) chunk_no = j; else chunk_no = get_rand_ul(0, num_chunks - 1); prep_rand_dest_write_unit(&wu, chunk_no); ret = do_write_chunk(fd, wu); if (ret < 0) goto child_bail; ret = log_write(&wu, log); if (ret < 0) goto child_bail; if (semaphore_v(sem_id) < 0) { ret = -1; goto child_bail; } usleep(10000); if (!(test_flags & DSCV_TEST)) continue; /* * Are you ready to crash the machine? */ if ((j > 1) && (j < num_chunks - 1)) { if (get_rand_ul(1, num_chunks) == num_chunks / 2) { if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } fprintf(stdout, "#%d process " "tries to crash the " "box.\n", getpid()); if (system("echo b>/proc/sysrq-trigger") < 0) { fprintf(stderr, "#%d process " "tries to enable sysrq-trigger " "but failed.\n", getpid()); goto child_bail; } } } else if (j == num_chunks - 1) { if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } fprintf(stdout, "#%d process " "tries to crash the " "box.\n", getpid()); if (system("echo b>/proc/sysrq-trigger") < 0) { fprintf(stderr, "#%d process " "tries to enable sysrq-trigger " "but failed.\n", getpid()); goto child_bail; } } } child_bail: if (fd) close(fd); if (ret > 0) ret = 0; exit(ret); } if (pid > 0) child_pid_list[i] = pid; } signal(SIGINT, sigint_handler); signal(SIGTERM, sigterm_handler); for (i = 0; i < num_children; i++) { waitpid(child_pid_list[i], &status, 0); ret = WEXITSTATUS(status); if (ret) { fprintf(stderr, "Child %d exits abnormally with " "RC=%d\n", child_pid_list[i], ret); } } if (fd) close(fd); if (sem_id) semaphore_close(sem_id); return ret; }