• Nov 19, 2022 •CodeCatch
0 likes • 0 views
#include <stdio.h> #include <string.h> #include <unistd.h> #define READ 0 /* The index of the read end of the pipe */ #define WRITE 1 /* The index of the write end of the pipe */ char * phrase = "This goes in the pipe"; int main() { int fd[2], bytesRead; char message[100]; /* Parent process' message buffer */ pipe(fd); /* Create unnamed pipe */ if (fork() == 0) /* Child, writer */ { close(fd[READ]); /* Close unused end */ write(fd[WRITE], phrase, strlen(phrase) + 1); /* Include NULL */ close(fd[WRITE]); /* Close used end */ } else /* Parent, reader */ { close(fd[WRITE]); /* Close unused end */ bytesRead = read(fd[READ], message, 100); printf("Parent just read %i bytes: %s\n", bytesRead, message); close(fd[READ]); /* Close used end */ } }
• Nov 18, 2022 •AustinLeath
#include <stdio.h> #include <stdlib.h> int main(){ int * int_ptr; int_ptr = (int *)malloc(2*sizeof(int)); if(!int_ptr) { printf("Something went wrong while allocating memory. Exiting..."); exit(1); } printf("Enter first integer: "); scanf("%i", &int_ptr[0]); printf("Enter second integer: "); scanf("%i", &int_ptr[1]); printf("Original values: 1st = %i 2nd = %i\n",int_ptr[0], int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); int_ptr[1] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[1]); int_ptr[0] = int_ptr[0] ^ int_ptr[1]; //printf("%i\n", int_ptr[0]); printf("Swapped values: 1st = %i 2nd = %i\n", int_ptr[0], int_ptr[1]); free(int_ptr); exit(0); }
0 likes • 2 views
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int glbvar = 6; int main() { int locvar = 88; pid_t pid; printf("Before fork()\n"); if ((pid = fork()) == 0) { /* child */ glbvar++; locvar++; } else if (pid > 0) { /* parent */ sleep(2); } else perror("fork error"); printf("pid=%d, glbvar=%d, locvar=%d\n", getpid(), glbvar, locvar); return 0; } /* end main */
#include <stdio.h> #include <unistd.h> int main() { char * cmd[] = { "who", "ls", "date" }; int i; printf("0=who 1=ls 2=date : "); scanf("%d", & i); execlp(cmd[i], cmd[i], (char * ) 0); printf("execlp failed\n"); return 0; }
• May 13, 2025 •LeifMessinger
0 likes • 4 views
#include <stdio.h> #include <stdlib.h> calculate(a, b) int a; int b; { printf("%d", a+b); } main(argc, argv) int argc; char** argv; { if(argc < 3){ return 0; } calculate(atoi(argv[1]), atoi(argv[2])); }
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pid; /* could be int */ int i; pid = fork(); printf("PID=%d\n", pid); if (pid > 0) { /* parent */ for (i = 0; i < 10; i++) printf("\t\t\tPARENT %d\n", i); } else { /* child */ for (i = 0; i < 10; i++) printf("CHILD %d\n", i); } return 0; }