int main() { int err; pthread_t tid; if ((err = mypthread_rwlock_rdlock(&rwlock)) != 0) { /* parent read locks entire file */ fprintf(stderr, "mypthread_rwlock_rdlock error: %s\n", strerror(err)); exit(1); } printf("%s: parent has read lock\n", gf_time()); if ((err = pthread_create(&tid, NULL, child_thread, NULL)) != 0) { fprintf(stderr, "pthread_create error: %s\n", strerror(err)); exit(1); } sleep(4); if ((err = mypthread_rwlock_unlock(&rwlock)) != 0) { fprintf(stderr, "mypthread_rwlock_unlock error: %s\n", strerror(err)); exit(1); } printf("%s: parent releases read lock\n", gf_time()); if ((err = pthread_join(tid, NULL)) != 0) { fprintf(stderr, "pthread_join error: %s\n", strerror(err)); exit(1); } exit(0); }
int main(int argc, char **argv) { int fd; pid_t childpid; if ((fd = open("test1.data", O_RDWR | O_CREAT, (mode_t) FILE_MODE)) == -1) { fprintf(stderr, "open error for %s: %s\n", "test1.data", strerror(errno)); exit(1); } if (read_lock(fd, 0, SEEK_SET, 0) == -1) { /* parent read locks entire file */ perror("read_lock error"); exit(1); } printf("%s: parent has read lock\n", gf_time()); if ((childpid = fork()) == -1) { perror("fork error"); exit(1); } if (childpid == 0) { /* 4child */ if (read_lock(fd, 0, SEEK_SET, 0) == -1) { /* this should work */ perror("read_lock error"); exit(1); } printf("%s: child has read lock\n", gf_time()); sleep(2); if (un_lock(fd, 0, SEEK_SET, 0) == -1) { perror("un_lock error"); exit(1); } printf("%s: child releases read lock\n", gf_time()); if (read_lock(fd, 0, SEEK_SET, 0) == -1) { /* this should work */ perror("read_lock error"); exit(1); } printf("%s: child has read lock\n", gf_time()); sleep(2); exit(0); } /* parent */ sleep(4); if (un_lock(fd, 0, SEEK_SET, 0) == -1) { perror("un_lock error"); exit(1); } printf("%s: parent releases read lock\n", gf_time()); exit(0); }
void *thread1(void *arg) { sleep(1); printf("%s: first child tries to obtain write lock\n", gf_time()); Pthread_rwlock_wrlock(&rwlock); /* this should block */ printf("%s: first child obtains write lock\n", gf_time()); sleep(2); Pthread_rwlock_unlock(&rwlock); printf("%s: first child releases write lock\n", gf_time()); return NULL; }
void *thread2(void *arg) { /* 4second child */ sleep(3); printf("%s: second child tries to obtain read lock\n", gf_time()); Pthread_rwlock_rdlock(&rwlock); printf("%s: second child obtains read lock\n", gf_time()); sleep(4); Pthread_rwlock_unlock(&rwlock); printf("%s: second child releases read lock\n", gf_time()); return NULL; }
void *thread1(void *arg) { int err; sleep(1); printf("%s: first child tries to obtain write lock\n", gf_time()); if ((err = pthread_rwlock_wrlock(&rwlock)) != 0) { /* this should block */ fprintf(stderr, "pthread_rwlock_wrlock error: %s\n", strerror(err)); exit(1); } printf("%s: first child obtains write lock\n", gf_time()); sleep(2); if ((err = pthread_rwlock_unlock(&rwlock)) != 0) { fprintf(stderr, "pthread_rwlock_unlock error: %s\n", strerror(err)); exit(1); } printf("%s: first child releases write lock\n", gf_time()); return NULL; }
void *thread2(void *arg) { int err; /* 4second child */ sleep(3); printf("%s: second child tries to obtain read lock\n", gf_time()); if ((err = pthread_rwlock_rdlock(&rwlock)) != 0) { fprintf(stderr, "pthread_rwlock_rdlock error: %s\n", strerror(err)); exit(1); } printf("%s: second child obtains read lock\n", gf_time()); sleep(4); if ((err = pthread_rwlock_unlock(&rwlock)) != 0) { fprintf(stderr, "pthread_rwlock_unlock error: %s\n", strerror(err)); exit(1); } printf("%s: second child releases read lock\n", gf_time()); return NULL; }
int main() { pthread_t tid1, tid2; Pthread_rwlock_wrlock(&rwlock); /* parent read locks entire file */ printf("%s: parent has write lock\n", gf_time()); Pthread_create(&tid1, NULL, thread1, NULL); Pthread_create(&tid2, NULL, thread2, NULL); /* 4parent */ sleep(5); Pthread_rwlock_unlock(&rwlock); printf("%s: parent releases write lock\n", gf_time()); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); exit(0); }
int main() { pthread_t tid1, tid2; int err; if ((err = pthread_rwlock_wrlock(&rwlock)) != 0) { /* parent read locks entire file */ fprintf(stderr, "pthread_rwlock_wrlock error: %s\n", strerror(err)); exit(1); } printf("%s: parent has write lock\n", gf_time()); if ((err = pthread_create(&tid1, NULL, thread1, NULL)) != 0) { fprintf(stderr, "pthread_create error: %s\n", strerror(err)); exit(1); } if ((err = pthread_create(&tid2, NULL, thread2, NULL)) != 0) { fprintf(stderr, "pthread_create error: %s\n", strerror(err)); exit(1); } /* 4parent */ sleep(5); if ((err = pthread_rwlock_unlock(&rwlock)) != 0) { fprintf(stderr, "pthread_rwlock_unlock error: %s\n", strerror(err)); exit(1); } printf("%s: parent releases write lock\n", gf_time()); if ((err = pthread_join(tid1, NULL)) != 0) { fprintf(stderr, "pthread_join error: %s\n", strerror(err)); exit(1); } if ((err = pthread_join(tid2, NULL)) != 0) { fprintf(stderr, "pthread_join error: %s\n", strerror(err)); exit(1); } exit(0); }
void *child_thread(void *arg) { /* 4child */ int err; if ((err = mypthread_rwlock_rdlock(&rwlock)) != 0) { /* this should work */ fprintf(stderr, "mypthread_rwlock_rdlock error: %s\n", strerror(err)); exit(1); } printf("%s: child has read lock\n", gf_time()); sleep(2); if ((err = mypthread_rwlock_unlock(&rwlock)) != 0) { fprintf(stderr, "mypthread_rwlock_unlock error: %s\n", strerror(err)); exit(1); } printf("%s: child releases read lock\n", gf_time()); if ((err = mypthread_rwlock_rdlock(&rwlock)) != 0) { /* this should work */ fprintf(stderr, "mypthread_rwlock_rdlock error: %s\n", strerror(err)); exit(1); } printf("%s: child has read lock\n", gf_time()); sleep(2); return NULL; }
void str_cli(FILE *fp, int sockfd) { int maxfdp1, val, stdineof; ssize_t n, nwritten; fd_set rset, wset; char to[MAXLINE], fr[MAXLINE]; char *toiptr, *tooptr, *friptr, *froptr; val = Fcntl(sockfd, F_GETFL, 0); Fcntl(sockfd, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDIN_FILENO, F_GETFL, 0); Fcntl(STDIN_FILENO, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDOUT_FILENO, F_GETFL, 0); Fcntl(STDOUT_FILENO, F_SETFL, val | O_NONBLOCK); toiptr = tooptr = to; /* initialize buffer pointers */ friptr = froptr = fr; stdineof = 0; maxfdp1 = max(max(STDIN_FILENO, STDOUT_FILENO), sockfd) + 1; for ( ; ; ) { FD_ZERO(&rset); FD_ZERO(&wset); if (stdineof == 0 && toiptr < &to[MAXLINE]) FD_SET(STDIN_FILENO, &rset); /* read from stdin */ if (friptr < &fr[MAXLINE]) FD_SET(sockfd, &rset); /* read from socket */ if (tooptr != toiptr) FD_SET(sockfd, &wset); /* data to write to socket */ if (froptr != friptr) FD_SET(STDOUT_FILENO, &wset); /* data to write to stdout */ Select(maxfdp1, &rset, &wset, NULL, NULL); /* end nonb1 */ /* include nonb2 */ if (FD_ISSET(STDIN_FILENO, &rset)) { if ( (n = read(STDIN_FILENO, toiptr, &to[MAXLINE] - toiptr)) < 0) { if (errno != EWOULDBLOCK) err_sys("read error on stdin"); } else if (n == 0) { #ifdef VOL2 fprintf(stderr, "%s: EOF on stdin\n", gf_time()); #endif stdineof = 1; /* all done with stdin */ if (tooptr == toiptr) Shutdown(sockfd, SHUT_WR);/* send FIN */ } else { #ifdef VOL2 fprintf(stderr, "%s: read %d bytes from stdin\n", gf_time(), n); #endif toiptr += n; /* # just read */ FD_SET(sockfd, &wset); /* try and write to socket below */ } } if (FD_ISSET(sockfd, &rset)) { if ( (n = read(sockfd, friptr, &fr[MAXLINE] - friptr)) < 0) { if (errno != EWOULDBLOCK) err_sys("read error on socket"); } else if (n == 0) { #ifdef VOL2 fprintf(stderr, "%s: EOF on socket\n", gf_time()); #endif if (stdineof) return; /* normal termination */ else err_quit("str_cli: server terminated prematurely"); } else { #ifdef VOL2 fprintf(stderr, "%s: read %d bytes from socket\n", gf_time(), n); #endif friptr += n; /* # just read */ FD_SET(STDOUT_FILENO, &wset); /* try and write below */ } } /* end nonb2 */ /* include nonb3 */ if (FD_ISSET(STDOUT_FILENO, &wset) && ( (n = friptr - froptr) > 0)) { if ( (nwritten = write(STDOUT_FILENO, froptr, n)) < 0) { if (errno != EWOULDBLOCK) err_sys("write error to stdout"); } else { #ifdef VOL2 fprintf(stderr, "%s: wrote %d bytes to stdout\n", gf_time(), nwritten); #endif froptr += nwritten; /* # just written */ if (froptr == friptr) froptr = friptr = fr; /* back to beginning of buffer */ } } if (FD_ISSET(sockfd, &wset) && ( (n = toiptr - tooptr) > 0)) { if ( (nwritten = write(sockfd, tooptr, n)) < 0) { if (errno != EWOULDBLOCK) err_sys("write error to socket"); } else { #ifdef VOL2 fprintf(stderr, "%s: wrote %d bytes to socket\n", gf_time(), nwritten); #endif tooptr += nwritten; /* # just written */ if (tooptr == toiptr) { toiptr = tooptr = to; /* back to beginning of buffer */ if (stdineof) Shutdown(sockfd, SHUT_WR); /* send FIN */ } } } } }
void str_cli(FILE * fd, int sockfd) { int maxfdp1, val, stdineof; ssize_t n, nwritten; fd_set rset, wset; char to[MAXLINE], fr[MAXLINE]; char *toiptr, *tooptr, *friptr, *froptr; val = Fcntl(sockfd, F_GETFL, 0); Fcntl(sockfd, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDIN_FILENO, F_GETFL, 0); Fcntl(STDIN_FILENO, F_SETFL, val | O_NONBLOCK); val = Fcntl(STDOUT_FILENO, F_GETFL, 0); Fcntl(STDOUT_FILENO, F_SETFL, val | O_NONBLOCK); toiptr = tooptr = &to[MAXLINE]; friptr = froptr = &fr[MAXLINE]; stdineof = 0; maxfdp1 = max(max(STDIN_FILENO, STDOUT_FILENO), sockfd) + 1; for(;;) { FD_ZERO(&rset); FD_ZERO(&wset); if(stdineof == 0 && toiptr < &to[MAXLINE]) FD_SET(STDIN_FILENO, &rset); if( friptr < &fr[MAXLINE] ) FD_SET(STDOUT_FILENO, &rset); if(toiptr != tooptr) FD_SET(sockfd, &wset); if(friptr != froptr) FD_SET(STDOUT_FILENO, &wset); Select(maxfdp1, &rset, &wset, NULL, NULL); if (FD_ISSET(STDIN_FILENO, &rset)) { if( (n = read(STDIN_FILENO, toiptr, &to[MAXLINE] - toiptr)) < 0) { if( errno != EWOULDBLOCK ) err_sys("read error on stdin"); } else if(n == 0) { fprintf(stderr, "%s: EOF on stdin\n", gf_time()); stdineof = 1; if (toiptr == tooptr) Shutdown(sockfd, SHUT_WR); } else { fprintf(stderr, "%s: read %d bytes from stdin\n", gf_time(), n); toiptr += n; FD_SET(sockfd, &wset); } } if(FD_ISSET(sockfd, &rset)) { if( (n = read(STDOUT_FILENO, friptr, &fr[MAXLINE] - friptr)) < 0) { if( errno != EWOULDBLOCK ) err_sys("read error on socket"); } else if(n == 0) { fprintf(stderr, "%s: EOF on stdin\n", gf_time()); if(stdineof) return; /* normal termination */ else err_quit("str_cli server terminated prematurely"); } else { fprintf(stderr, "%s: read %d bytes from socket\n", gf_time(), n); friptr += n; FD_SET(STDOUT_FILENO, &wset); } } if(FD_ISSET(STDOUT_FILENO, &wset) && ( n = (friptr - froptr)) > 0) { if( (nwritten= write(STDOUT_FILENO, froptr, n)) < 0) { if(errno != EWOULDBLOCK) err_sys("write error to stdout"); } else { fprintf(stderr, "%s: wrote %d bytes to stdout\n", gf_time(), nwritten); froptr += n; if(froptr == friptr) froptr = friptr = fr; } } if(FD_ISSET(sockfd, &wset) && ( n = (toiptr - tooptr)) > 0) { if( (nwritten= write(STDOUT_FILENO, froptr, n)) < 0) { if(errno != EWOULDBLOCK) err_sys("write error to socket"); } else { fprintf(stderr, "%s: wrote %d bytes to socket\n", gf_time(), nwritten); tooptr += n; if(tooptr == toiptr) { tooptr = toiptr = to; if(stdineof) Shutdown(sockfd, SHUT_WR); } } } } }