Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shibam Saha committed Nov 23, 2022
1 parent eba6971 commit 302b43b
Show file tree
Hide file tree
Showing 30 changed files with 1,056 additions and 327 deletions.
8 changes: 4 additions & 4 deletions 01_Write.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Q. To write some data on the standard output device (by default monitor)
// Q. To write some data on the standard output device (by default monitor)

#include <stdio.h>
#include <unistd.h>

int main()
{
// This contains number of bytes read
int count;
int count;

// 1 --> The file descriptor (stdout)
// "Hello Linux!\n" --> The data
Expand All @@ -18,10 +18,10 @@ int main()

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 01_Write.c
s4shibam@SHIBAM:~/OS$ ./a.out
Hello Linux!
Hello Linux!
Total bytes written: 13.
s4shibam@SHIBAM:~/OS$
*/
22 changes: 11 additions & 11 deletions 02_Read.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
#include <stdio.h>
#include <unistd.h>

int main ()
int main()
{
// This contains number of bytes read
int len;
int len;

// Read maximum 50 bytes from standard input device (0 --> stdin)
// & store in buffer (ch)
char ch[50];
len = read (0, ch, 50);
len = read(0, ch, 50);

// Write "len" bytes from the buffer on screen
write (1, ch, len);
printf ("\nTotal bytes written: %d.\n", len);
write(1, ch, len);
printf("\nTotal bytes written: %d.\n", len);
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 2_read.c
Output:
s4shibam@SHIBAM:~/OS$ gcc 2_read.c
s4shibam@SHIBAM:~/OS$ ./a.out
Hello Dear Linux!
Hello Dear Linux!
Total bytes written: 18.
s4shibam@SHIBAM:~/OS$
*/
21 changes: 11 additions & 10 deletions 03_Open_And_Read_Existing_File.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
// Q. Write a program using open() system call to read data from (max 50 char)
// an existing file Text1.txt and print them on screen.
// an existing file Text1.txt and print them on screen.

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

int main (){

int main()
{

int len, fd;
char ch[50];

// Opens test.txt in read mode and
// the file descriptor is saved in integer fd
fd = open("Text1.txt", O_RDONLY);
fd = open("Text1.txt", O_RDONLY);

// The value of the file descriptor is printed.
printf("The file descriptor of the file is: %d\n", fd);
printf("The file descriptor of the file is: %d\n", fd);

// Read maximum 50 characters from the file pointed by file descriptor fd
// and save them in buffer (ch)
len = read(fd, ch, 50);

// Write on the screen from the buffer
write(1, ch, len);
write(1, ch, len);
printf("\nTotal bytes written: %d.\n", len);
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 3_Open_And_Read_Existing_File.c
s4shibam@SHIBAM:~/OS$ ./a.out
The file descriptor of the file is: 3
Expand Down
25 changes: 13 additions & 12 deletions 04_Open_Read_And_Write_In_New_File.c
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
// Q. Write a program using open() system call to read data from (max 50 char)
// an existing file Text1.txt and write them into non-existing file Text2.txt
// an existing file Text1.txt and write them into non-existing file Text2.txt

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

int main (){

int main()
{

int len, fd, fd1;
char ch[50];

// Opens test.txt in read mode and
// the file descriptor is saved in integer fd
fd = open("Text1.txt", O_RDONLY);
fd = open("Text1.txt", O_RDONLY);

// The value of the file descriptor is printed.
printf("The file descriptor of the file is: %d\n", fd);
printf("The file descriptor of the file is: %d\n", fd);

// Read maximum 50 characters from the file pointed by file descriptor fd
// and save them in buffer (ch)
len = read(fd, ch, 50);

// New file is created in write only mode and
// the file descriptor is saved in integer fd1
// Pipe symbol (|) is used to separate O_WRONLY and O_CREAT
fd1 = open("Text2.txt", O_WRONLY | O_CREAT, 0642);

// Write in the Text2.txt file from the buffer
write(fd1, ch, len);
write(fd1, ch, len);
printf("\nTotal bytes written: %d.\n", len);
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 4_Open_Read_And_Write_In_New_File.c
s4shibam@SHIBAM:~/OS$ ./a.out
The file descriptor of the file is: 3
Total bytes written: 23.
s4shibam@SHIBAM:~/OS$
s4shibam@SHIBAM:~/OS$
*/
28 changes: 14 additions & 14 deletions 05_Open_Read_And_Append_In_Existing_File.c
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
// Q. Write a program using open() system call to read data from (max 50 char)
// the standard input device and append them into existing file “Text3.txt”

// the standard input device and append them into existing file �Text3.txt�

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

int main (){

int main()
{

int len, fd, fd1;
char ch[50];
char append[50];

// Read maximum 50 bytes from standard input device (0 --> stdin)
// & store in buffer (ch)
len = read(0, append, 50);
printf("Total bytes read: %d.\n", len);

printf("Total bytes read: %d.\n", len);

// Existing file is opened in append mode and
// the file descriptor is saved in integer fd
// Pipe symbol (|) is used to separate O_WRONLY and O_APPEND
fd = open("Text3.txt", O_WRONLY | O_APPEND);

// Append in the Text3.txt file from the buffer
write(fd, append, len);
write(fd, append, len);
printf("\nTotal bytes appended: %d.\n", len);
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 5_Open_Read_And_Append_In_Existing_File.c
s4shibam@SHIBAM:~/OS$ gcc 5_Open_Read_And_Append_In_Existing_File.c
s4shibam@SHIBAM:~/OS$ ./a.out
This is the appended line!
Total bytes read: 27.
Total bytes appended: 27.
s4shibam@SHIBAM:~/OS$
s4shibam@SHIBAM:~/OS$
*/
53 changes: 28 additions & 25 deletions 06_Create_Child_Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@
int main()
{
int f;
printf ("Before fork!\n");
f = fork();

printf("Before fork!\n");
f = fork();

// The creation of the process was unsuccessful
if (f < 0){

printf ("Error occurred!\n");
if (f < 0)
{

printf("Error occurred!\n");
}

// Child process
else if (f == 0){

printf ("This is Child Process!\n");
printf ("Child: Child Process pid: %d\n", getpid());
printf ("Child: Parent Process pid: %d\n", getppid());
else if (f == 0)
{

printf("This is Child Process!\n");
printf("Child: Child Process pid: %d\n", getpid());
printf("Child: Parent Process pid: %d\n", getppid());
}

// Parent process
else {

printf ("This is Parent Process!\n");
printf ("Parent: Parent Process pid: %d\n", getpid());
printf ("Parent: Child Process pid: %d\n", f);
else
{

printf("This is Parent Process!\n");
printf("Parent: Parent Process pid: %d\n", getpid());
printf("Parent: Child Process pid: %d\n", f);
}
// This part will be executed by both child & parent
printf ("This is common!\n");

// This part will be executed by both child & parent
printf("This is common!\n");
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 6_Create_Child_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out
Before fork!
Expand All @@ -51,6 +54,6 @@ int main()
Parent: Child Process pid: 1869
Child: Parent Process pid: 1868
This is common!
This is common!
s4shibam@SHIBAM:~/OS$
This is common!
s4shibam@SHIBAM:~/OS$
*/
53 changes: 28 additions & 25 deletions 07_Wait_In_Process.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,44 @@
int main()
{
int f;
printf ("Before fork!\n");
f = fork();

printf("Before fork!\n");
f = fork();

// The creation of the process was unsuccessful
if (f < 0){

printf ("Error occurred!\n");
if (f < 0)
{

printf("Error occurred!\n");
}

// Child process
else if (f == 0){

printf ("This is Child Process!\n");
printf ("Child: Child Process pid: %d\n", getpid());
printf ("Child: Parent Process pid: %d\n", getppid());
else if (f == 0)
{

printf("This is Child Process!\n");
printf("Child: Child Process pid: %d\n", getpid());
printf("Child: Parent Process pid: %d\n", getppid());
}

// Parent process
else {

else
{

// Parent process will wait until execution of the child process
wait(NULL);
printf ("This is Parent Process!\n");
printf ("Parent: Parent Process pid: %d\n", getpid());
printf ("Parent: Child Process pid: %d\n", f);
printf("This is Parent Process!\n");
printf("Parent: Parent Process pid: %d\n", getpid());
printf("Parent: Child Process pid: %d\n", f);
}
// This part will be executed by both child & parent
printf ("This is common!\n");

// This part will be executed by both child & parent
printf("This is common!\n");
}

/*
Output:
s4shibam@SHIBAM:~/OS$ gcc 7_Wait_In_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out
Before fork!
Expand All @@ -54,6 +57,6 @@ int main()
Parent: Parent Process pid: 1979
Parent: Child Process pid: 1980
This is common!
s4shibam@SHIBAM:~/OS$
s4shibam@SHIBAM:~/OS$
*/
Loading

0 comments on commit 302b43b

Please sign in to comment.