From b6d6118335954563fbff88745848b01a22040827 Mon Sep 17 00:00:00 2001 From: Jaimandeep Singh Date: Sun, 2 Feb 2020 16:00:48 +0530 Subject: [PATCH] added swayam lecture notes --- swayam-os-2020/week_01/ipc_files.c | 39 ++++++++++++++++++ swayam-os-2020/week_01/pipes_basic.c | 39 ++++++++++++++++++ swayam-os-2020/week_01/pipes_stdin_stdout.c | 45 +++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 swayam-os-2020/week_01/ipc_files.c create mode 100644 swayam-os-2020/week_01/pipes_basic.c create mode 100644 swayam-os-2020/week_01/pipes_stdin_stdout.c diff --git a/swayam-os-2020/week_01/ipc_files.c b/swayam-os-2020/week_01/ipc_files.c new file mode 100644 index 0000000..ac1a7f0 --- /dev/null +++ b/swayam-os-2020/week_01/ipc_files.c @@ -0,0 +1,39 @@ +/* This program shares file discriptors for Inter Process +Communictions (IPC) */ + +#include +#include +#include +#include +#include +#include + +int main(int argc, char const *argv[]) { + + // if file foo.txt does not exist then it is created with permission of 644. Prefix by 0 for octal. + int fd = open("foo.txt", O_RDWR | O_CREAT, 0644); + if (fd ==-1) + // print error code defined in error.h + printf("Error Number %d\n", errno); + + printf("File Opened with file descriptor (fd) = %d \n", fd); + + // create copy of the process + int pid = fork(); + if (pid == 0) { + sleep(5); // Wait for Parent to write into the file + char buff[100]; + lseek(fd, 0, SEEK_SET); + read(fd, buff, 100); + printf("Read from Child. Data = %s\n", buff); + } else if(pid > 0) { + // The wait() system call suspends execution of the current process until + // one of its children terminates. + // sleep(5); // strange behaviour if sleep is put here + write(fd, "Hello", 5); + wait(0); + } else + printf("error in fork %d\n", errno); + + return 0; +} diff --git a/swayam-os-2020/week_01/pipes_basic.c b/swayam-os-2020/week_01/pipes_basic.c new file mode 100644 index 0000000..0290551 --- /dev/null +++ b/swayam-os-2020/week_01/pipes_basic.c @@ -0,0 +1,39 @@ +/* This program creates a pipe which is a unidirectional data channel that can be used + for interprocess communication (IPC). */ + +#include +#include +#include +#include +#include + +int main(int argc, char const *argv[]) { + + int pipefd[2]; + + if (pipe(pipefd) == -1) + // print error code defined in error.h + printf("Could No create pipe. Error Number %d\n", errno); + + printf("File Opened with file descriptor (pipefd[0] = %d) and pipefd[1] = %d \n", + pipefd[0], pipefd[1]); + + // create copy of the process + int pid = fork(); + if (pid == 0) { + + close(pipefd[1]); //close write end of file + + char buff[100]; + read(pipefd[0], buff, 100); + printf("Read from Child = %s\n", buff); + } else if(pid > 0) { + + close(pipefd[0]); // close read end of pipe + write(pipefd[1], "Hello", 5); + wait(0); // wait for child process to exit + } else + printf("error in fork %d\n", errno); + + return 0; +} diff --git a/swayam-os-2020/week_01/pipes_stdin_stdout.c b/swayam-os-2020/week_01/pipes_stdin_stdout.c new file mode 100644 index 0000000..3d00717 --- /dev/null +++ b/swayam-os-2020/week_01/pipes_stdin_stdout.c @@ -0,0 +1,45 @@ +/* This program creates a pipe which is a unidirectional data channel that can be used + for interprocess communication (IPC). */ + +#include +#include +#include +#include +#include + +int main(int argc, char const *argv[]) { + + int pipefd[2]; + + if (pipe(pipefd) == -1) + // print error code defined in error.h + printf("Could No create pipe. Error Number %d\n", errno); + + printf("File Opened with file descriptor (pipefd[0] = %d) and pipefd[1] = %d \n", + pipefd[0], pipefd[1]); + + // create copy of the process + int pid = fork(); + if (pid == 0) { + + close(pipefd[1]); //close write end of file + close(0); //close standard input + dup(pipefd[0]); //Now STDIN and pipfd[0] point to the same data structure + + char buff[100]; + scanf("%s", buff); + //read(pipefd[0], buff, 100); + printf("Read from Child = %s \n", buff); + } else if(pid > 0) { + + close(pipefd[0]); // close read end of pipe + close(1); + dup(pipefd[1]); + //write(pipefd[1], "Hello", 5); + printf("Hello \n"); + wait(0); + } else + printf("error in fork %d\n", errno); + + return 0; +}