コード例 #1
0
int main(void) {
    int chat;
    char chatmsg;
    pid_t pid;
    int file;
    int i = 0;
    int child_status;

    if(!(file = open("file.txt", O_CREAT|O_RDWR|O_APPEND))) {
        exit(-1);
    }

    pid = fork();
    if(pid == 0) {
        chat = OpenChatFellow();
        while(read(chat,&chatmsg,1) == 1) {
            write(STDOUT_FILENO, &chatmsg, 1);
            write(file, &chatmsg, 1);
            i++;
        }
        close(chat);
        exit(i);
    }
    else {
        while(read(STDIN_FILENO, &chatmsg, 1) == 1) {
            write(STDOUT_FILENO, &chatmsg, 1);
            if(chatmsg == 'q') {
                break;
            }
        }

        /* wait for the child process */
        wait(&child_status);
        printf("number of characters written by child process: %d\n", WEXITSTATUS(child_status));
        printf("dumping file contents...\n");
        /* go to the beginning of the file and dump the content to stdout */
        lseek(file, 0L, SEEK_SET);
        while(read(file,&chatmsg,1) == 1) {
            write(STDOUT_FILENO, &chatmsg, 1);
            if(i%7 == 6) {
                write(STDOUT_FILENO, "\n", 1);
            }
            i++;
        }
        printf("\ndone\n");
    }


    close(file);
    return 0;
}
コード例 #2
0
ファイル: lab5B1.c プロジェクト: jy03189211/realtime
int main(int argc, const char * argv[])
{
    
    int fellow_desc,r;
    char chr_fellow = '\0',chr_own;
    fellow_desc=OpenChatFellow();
    
    pid_t pid;
    pid=fork();
    

    //child
    if(pid==0)
    {
        while ((r=read(fellow_desc, &chr_fellow, 1))>0) {

            
            write(1,&chr_fellow,1);
            
        }
        exit(0);
    }
    
    //parent
    while(chr_own!='q'&&chr_own!='Q'){
        
        read(0,&chr_own,1);
        
        write(1,&chr_own,1);
    }

    if(wait(NULL)>0){
        printf("\nchild terminted!\n");
    }
    
    
    
    return 0;
}
コード例 #3
0
ファイル: lab3.c プロジェクト: AwelEshetu/school_projects
int main(void){
/*//printf("i");
//char ch='2';
//fflush(stdout);
//write(1,'1',1);
while(1){
write(1,&ch,1);
}*/
int fellow_desc;
char char_fellow;
int n;
fellow_desc=OpenChatFellow();
n=read(fellow_desc,&char_fellow,1);
while(n>0){
write(1,&char_fellow,1);
n=read(fellow_desc,&char_fellow,1);
}
close(fellow_desc);
return 0;


}
コード例 #4
0
int main(void) {
    pid_t childPid = -1;
    int status;
    int fellow_desc = -1;
    int file_flags;
    int result = 0;
    char fellow_chr;
    char keyboard_chr;

    fellow_desc = OpenChatFellow();
    childPid = fork();
    if (childPid > 0) { //this is parent process
        /* read characters from the terminal
         * if q or Q is entered, stop reading from the terminal
         * wait for child process to terminate
         * display "Child terminated"
         */
        //change STDOUT_FILENO file descriptor from blocked mode to unblocked mode
        file_flags = fcntl(STDOUT_FILENO, F_GETFL);
        file_flags = file_flags | O_NONBLOCK;
        fcntl(STDOUT_FILENO, F_SETFL, file_flags);

        do {
            result = read(STDIN_FILENO, &keyboard_chr, 1);
            if (result != -1)
                write(STDOUT_FILENO, &keyboard_chr, 1);
        } while (keyboard_chr != 'Q' && keyboard_chr != 'q');

        printf("\nWait for child process to terminate.\n");
        wait(&status); //wait for child process to terminate
        printf("\nChild terminated.\n");
        close(STDOUT_FILENO);
    } else if (childPid == 0) { //this is child process
        /* read the inputs from the chat fellow
         * display the input character to the terminal
         * write the characters read to a file
         * terminate if eof is encountered
         */
        int fd = -1;

        fd = open("/tmp/file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
        if (fd == -1 ) {
            perror("Open");
            exit(EXIT_FAILURE);
        }

        do {
            result = read(fellow_desc, &fellow_chr, 1);
            if (result != -1) {
                write(STDOUT_FILENO, &fellow_chr, 1);
                write(fd, &fellow_chr, 1);
            }
        } while (fellow_chr != 'z');

        close(fd);
        close(fellow_desc);
        exit(EXIT_SUCCESS);
    } else
        perror("fork() error");

    return EXIT_SUCCESS;
}
コード例 #5
0
ファイル: lab35.c プロジェクト: AwelEshetu/school_projects
int main(void){
/*//printf("i");
//char ch='2';
//fflush(stdout);
//write(1,'1',1);
while(1){
write(1,&ch,1);
}*/
int fellow_desc;
char chr_fellow;
char chr_kb;
int file_flags,result;
fellow_desc=OpenChatFellow();
//put keyboard to unblocked mode
file_flags=fcntl(0,F_GETFL); // read current file flags
file_flags=file_flags|O_NONBLOCK; // add o_NONBLOCK bit
fcntl(0,F_SETFL,file_flags); //write new flags back
file_flags=fcntl(fellow_desc,F_GETFL);
file_flags=file_flags|O_NONBLOCK;
fcntl(fellow_desc,F_SETFL,file_flags);
while(1)
{
//keyboard
result=read(0,&chr_kb,1);
if(result>0)
{
if(chr_kb=='Q'||chr_kb=='q')
exit(0);
else{
printf("%c",chr_kb);
fflush(stdout);
}
}
if(result==-1 && errno!=EAGAIN)
perror("keyboard error");
//Fellow input
result=read(fellow_desc,&chr_fellow,1);
if(result>0)
{
print("%c",chr_fellow);
fflush(stdout);
}
if (result==-1 && errno!=EAGAIN)
perror("Fellow error");
if(result==0) //End of file
exit(0);
}
/*n=read(fellow_desc,&char_fellow,1);
while(n>0){
write(1,&char_fellow,1);
n=read(0,&chr,1);
if (n>0)
write(1,&chr,1);
if (n==-1 && errno !=EAGAIN)
perror("keyboard read error!");
// we don't care the situation where n==-1 and errno==EAGAIN
n=read(fellow_desc,&char_fellow,1);
}*/

return EXIT_SUCCESS;


}