Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions swayam-os-2020/week_01/ipc_files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This program shares file discriptors for Inter Process
Communictions (IPC) */

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

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;
}
39 changes: 39 additions & 0 deletions swayam-os-2020/week_01/pipes_basic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This program creates a pipe which is a unidirectional data channel that can be used
for interprocess communication (IPC). */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

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;
}
45 changes: 45 additions & 0 deletions swayam-os-2020/week_01/pipes_stdin_stdout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This program creates a pipe which is a unidirectional data channel that can be used
for interprocess communication (IPC). */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

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;
}