Created
March 12, 2020 23:58
-
-
Save tolgahanakgun/202345c6a0ab6f714960dc1815eb7ba2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C program to illustrate | |
// non I/O blocking calls | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> // library for fcntl function | |
#define MSGSIZE 16 | |
char* msg1 ="hello"; | |
char* msg2 ="bye !!"; | |
void parent_read(int p[]); | |
void child_write(int p[]); | |
int main() | |
{ | |
int p[2], i; | |
// error checking for pipe | |
if (pipe(p) < 0) | |
exit(1); | |
// error checking for fcntl | |
//if (fcntl(p[0], F_SETFL, fcntl(p[0], F_GETFL, 0) & ~O_NONBLOCK) < 0) | |
if (fcntl(p[0], F_SETFL, O_NONBLOCK) < 0) | |
exit(2); | |
// continued | |
switch (fork()) { | |
// error | |
case -1: | |
exit(3); | |
// 0 for child process | |
case 0: | |
child_write(p); | |
break; | |
default: | |
parent_read(p); | |
break; | |
} | |
return 0; | |
} | |
void parent_read(int p[]) | |
{ | |
int nread; | |
char buf[MSGSIZE]; | |
struct timeval master_tv, temp_tv; | |
master_tv.tv_sec = 5; | |
master_tv.tv_usec = 0; | |
fd_set master_readfds, temp_readfds; | |
FD_ZERO(&master_readfds); | |
FD_SET(p[0], &master_readfds); | |
FD_SET(STDIN_FILENO, &master_readfds); | |
// write link | |
//close(p[1]); | |
while (1) { | |
memcpy(&temp_readfds, &master_readfds, sizeof(master_readfds)); | |
memcpy(&temp_tv, &master_tv, sizeof(master_tv)); | |
memset(buf, '\0', MSGSIZE * sizeof(char)); | |
nread = select(FD_SETSIZE, &temp_readfds, NULL, NULL, &temp_tv); | |
printf("nread:%d\n",nread); | |
if(nread == 0){ | |
printf("Timeout\n"); | |
} | |
if (FD_ISSET(p[0], &temp_readfds)){ | |
nread = read(p[0], buf, MSGSIZE); | |
printf("buf:%s\n", buf); | |
} | |
if (FD_ISSET(STDIN_FILENO, &temp_readfds)){ | |
nread = read(STDIN_FILENO, buf, MSGSIZE - 1); | |
buf[strcspn(buf, "\n")] = '\0'; | |
printf("klavyeden %s girildi\n", buf); | |
} | |
// // read call if return -1 then pipe is | |
// // empty because of fcntl | |
// nread = read(p[0], buf, MSGSIZE); | |
// switch (nread) { | |
// case -1: | |
// printf("(pipe empty)\n"); | |
// //sleep(1); | |
// break; | |
// // case 0 means all bytes are read and EOF(end of conv.) | |
// case 0: | |
// printf("End of conversation\n"); | |
// // read link | |
// close(p[0]); | |
// exit(0); | |
// default: | |
// // text read | |
// // by default return no. of bytes | |
// // which read call read at that time | |
// printf("MSG = % s\n", buf); | |
// } | |
} | |
} | |
void child_write(int p[]) | |
{ | |
int i; | |
// read link | |
// close(p[0]); | |
// write 3 times "hello" in 3 second interval | |
for (i = 0; i < 3; i++) { | |
write(p[1], msg1, MSGSIZE); | |
sleep(3); | |
} | |
// write "bye" one times | |
write(p[1], msg2, MSGSIZE); | |
// here after write all bytes then write end | |
// doesn't close so read end block but | |
// because of fcntl block doesn't happen.. | |
exit(0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment