-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
067527d
commit 12200a9
Showing
14 changed files
with
277 additions
and
13 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
CSE344 - System Programming/Coding Practices/.vscode/settings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
192
CSE344 - System Programming/Coding Practices/practice47.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
#include <time.h> | ||
|
||
// hexdump | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
unsigned char buf[16]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
hello world from child | ||
hi world from parent |