• 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 */ } }
// The following code is the fast inverse square root implementation from Quake III Arena // this code has been stripped of C preprocessor directives, but includes the exact original comment text float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; }
0 likes • 2 views
#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; }
0 likes • 1 view
#include <stdio.h> #include <assert.h> #include <signal.h> void myHandler(int iSig) { printf("In myHandler with argument %d\n", iSig); } int main() { void( * pfRet)(int) = signal(SIGINT, myHandler); assert(pfRet != SIG_ERR); printf("Entering an infinite loop\n"); while (1) { printf("."); } return 0; // use CTRL+\ to exit }
#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 • 3 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])); }