Skip to content

Commit

Permalink
keep practicing
Browse files Browse the repository at this point in the history
  • Loading branch information
yusufarsln98 committed May 10, 2023
1 parent 067527d commit 12200a9
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.errorSquiggles": "disabled"
}
Binary file not shown.
Binary file not shown.
1 change: 0 additions & 1 deletion CSE344 - System Programming/Coding Practices/practice13.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ int main(int argc, char *argv[])
kill(pid, SIGKILL);
wait(&status);
printf("Parent process is terminated.\n");
// print pid
}
else
{
Expand Down
7 changes: 0 additions & 7 deletions CSE344 - System Programming/Coding Practices/practice16.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ void critical_operation()
}
printf("Critical operation complete.\n");
}

void sigint_handler(int sig)
{
printf("Received SIGINT signal.\n");
}
/*
Signal for segmentation violation (SIGSEGV) cannot be masked or handled.
Also, SIGSTOP and SIGKILL cannot be blocked or handled.
Expand All @@ -49,8 +44,6 @@ int main()
sigaddset(&mask, SIGINT); // add SIGINT to the signal set
sigaddset(&mask, SIGTSTP); // add SIGTSTP to the signal set
sigprocmask(SIG_BLOCK, &mask, NULL); // block the signals in the signal set
signal(SIGINT, sigint_handler); // install the signal handler for SIGINT (see after critical_operation() is done)

// Pressing Ctrl+C will not interrupt the program
printf("Press Ctrl+C to interrupt the program...\n");

Expand Down
1 change: 1 addition & 0 deletions CSE344 - System Programming/Coding Practices/practice26.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ int main()

sigfillset(&sigset); // fills the set with all signals
sigdelset(&sigset, SIGUSR1); // deletes SIGUSR1 from the set
// sigdelset(&sigset, SIGINT); // deletes SIGUSR1 from the set

time(&t); // gets the current time
printf("parent waiting for child to send SIGUSR1 at %s", ctime(&t));
Expand Down
2 changes: 1 addition & 1 deletion CSE344 - System Programming/Coding Practices/practice37.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <sys/wait.h>
#include <stdlib.h>

int system(char *command)
int my_system(char *command)
{
pid_t pid;
int status;
Expand Down
4 changes: 2 additions & 2 deletions CSE344 - System Programming/Coding Practices/practice38.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// add mask to the signal set

int system(char *command)
int my_system(char *command)
{
sigset_t block_mask, orig_mask;
struct sigaction sa_ignore, sa_orig_quit, sa_orig_int, sa_default;
Expand All @@ -18,7 +18,7 @@ int system(char *command)

if (command == NULL) // Is a shell available?
{
return system(":") == 0;
return my_system(":") == 0;
}

sigemptyset(&block_mask); // Block SIGCHLD
Expand Down
2 changes: 1 addition & 1 deletion CSE344 - System Programming/Coding Practices/practice4.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

int main(int argc, char *argv[])
{
// create a file
// create a file if does not exists
int fd = open("test.txt", O_CREAT | O_WRONLY, 0644);
if (fd == -1)
{
Expand Down
48 changes: 48 additions & 0 deletions CSE344 - System Programming/Coding Practices/practice45.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <stdio.h>
#include <unistd.h>

int main()
{
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int pid;
char pipe1writemessage[20] = "Hi";
char pipe2writemessage[20] = "Hello";
char readmessage[20];
returnstatus1 = pipe(pipefds1);

if (returnstatus1 == -1)
{
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);

if (returnstatus2 == -1)
{
printf("Unable to create pipe 2 \n");
return 1;
}
pid = fork();

if (pid != 0)
{
// Parent process
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("In Parent: Writing to pipe 1 - Message is %s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 - Message is %s\n", readmessage);
}
else
{ // child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 - Message is %s\n", readmessage);
printf("In Child: Writing to pipe 2 - Message is %s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;
}
27 changes: 27 additions & 0 deletions CSE344 - System Programming/Coding Practices/practice46.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
popen and pclose functions:
#include <stdio.h>
FILE *popen(const char* command, const char* mode); // returns file stream
// or NULL on error
int pclose(FILE *stream); // Returns termination
// status of child process
// or -1 on error
since popen creates a shell and executes it, it is risky to use it
as system() function
*/

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

int main(int argc, char const *argv[])
{
FILE *stream = popen("sort", "w");
fprintf(stream, "This is a test.\n");
fprintf(stream, "Hello, world.\n");
fprintf(stream, "My dog has fleas.\n");
fprintf(stream, "This program is great.\n");
fprintf(stream, "One fish, two fish.\n");
return pclose(stream);
}
192 changes: 192 additions & 0 deletions CSE344 - System Programming/Coding Practices/practice47.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
FIFOs
A FIFO is simply a pipe that has a name in the filesystem.
* it is unidirectional (has reading and writing ends)
* unlike the Pipes, in FIFO processes does not need to be related.
#include <sys/stat.h>
int mkfifo(const char* pathname, mode_t mode); // returns 0 on success, -1 otherwise
modes: constants: S_I(R|W|X)(USR|GRP|OTH)
*/

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

#define BUF_SIZE 256
#define FIFO_PERM (S_IRUSR | S_IWUSR)

// r_write function
ssize_t r_write(int fd, void *buf, size_t size)
{
ssize_t retval;
ssize_t wcnt = 0;

while (size > 0 && (retval = write(fd, buf, size)) != 0)
{
if (retval == -1)
{
if (errno == EINTR)
continue;
perror("write");
break;
}
size -= retval;
buf += retval;
wcnt += retval;
}

return wcnt;
}

// r_read function
ssize_t r_read(int fd, void *buf, size_t size)
{
ssize_t retval;
ssize_t rcnt = 0;

while (size > 0 && (retval = read(fd, buf, size)) != 0)
{
if (retval == -1)
{
if (errno == EINTR)
continue;
perror("read");
break;
}
size -= retval;
buf += retval;
rcnt += retval;
}

return rcnt;
}

int dofifochild(const char *fifoname, const char *idstring);
int dofifoparent(const char *fifoname);

int main(int argc, char const *argv[])
{
pid_t childpid;

if (argc != 2)
{
fprintf(stderr, "Usage: %s pipename\n", argv[0]);
return 1;
}

if (mkfifo(argv[1], FIFO_PERM) == -1)
{
if (errno != EEXIST)
{
fprintf(stderr, "[%ld]: failed to create named pipe %s: %s\n",
(long)getpid(), argv[1], strerror(errno));
return 1;
}
}

if ((childpid = fork()) == -1)
{
perror("Failed to fork");
return 1;
}

if (childpid == 0)
{
// the child writes
return dofifochild(argv[1], "this was written by the child");
}
else
{
return dofifoparent(argv[1]);
}

return 0;
}

int dofifochild(const char *fifoname, const char *idstring)
{
char buf[BUF_SIZE];
int fd;
int rval;
ssize_t strsize;

fprintf(stderr, "[%ld]: (child) about the open FIFO %s. . .\n",
(long)getpid(), fifoname);

while (((fd = open(fifoname, O_WRONLY)) == -1) && (errno == EINTR))
;

if (fd == -1)
{
fprintf(stderr, "[%ld]: failed to open named pipe %s for write: %s\n",
(long)getpid(), fifoname, strerror(errno));
return 1;
}

rval = snprintf(buf, BUF_SIZE, "[%ld]: %s\n", (long)getpid(), idstring);

if (rval < 0)
{
fprintf(stderr, "[%ld]: failed to make the string: %s\n",
(long)getpid(), strerror(errno));
return 1;
}

strsize = strlen(buf) + 1;

fprintf(stderr, "[%ld]: about to write...\n", (long)getpid());

rval = r_write(fd, buf, strsize);

if (rval != strsize)
{
fprintf(stderr, "[%ld]: failed to write to pipe: %s\n",
(long)getpid(), strerror(errno));
return 1;
}

fprintf(stderr, "[%ld]: write finished\n", (long)getpid());
return 0;
}

// dofifoparent implementation
int dofifoparent(const char *fifoname)
{
char buf[BUF_SIZE];
int fd;
int rval;

fprintf(stderr, "[%ld]: (parent) about to open FIFO %s...\n",
(long)getpid(), fifoname);

while (((fd = open(fifoname, O_RDONLY)) == -1) && (errno == EINTR))
;

if (fd == -1)
{
fprintf(stderr, "[%ld]: failed to open named pipe %s for read: %s\n",
(long)getpid(), fifoname, strerror(errno));
return 1;
}

fprintf(stderr, "[%ld]: about to read...\n", (long)getpid());

rval = r_read(fd, buf, BUF_SIZE);

if (rval == -1)
{
fprintf(stderr, "[%ld]: failed to read from pipe: %s\n",
(long)getpid(), strerror(errno));
return 1;
}
fprintf(stderr, "[%ld]: read %.*s\n", (long)getpid(), rval, buf);

return 0;
}
1 change: 0 additions & 1 deletion CSE344 - System Programming/Coding Practices/practice7.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <time.h>

// hexdump

int main(int argc, char *argv[])
{
unsigned char buf[16];
Expand Down
2 changes: 2 additions & 0 deletions CSE344 - System Programming/Coding Practices/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello world from child
hi world from parent

0 comments on commit 12200a9

Please sign in to comment.