inner fork() variables
0 likes • Nov 19, 2022
C
Loading...
More C Posts
// 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 textfloat 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 hackingi = 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 removedreturn y;}
#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 */}}
//===============replaceString.c===============#include "replaceString.h"//Normally I'd make a replaceStringMalloc to return a malloced string or a replaceStringMut to change the original string, but I'm not satisfied with those function names so it's best just to do to = (char*)malloc(replaceStringLength(from, replace, replacement)); replaceString(to, from, replace, replacement);//If this was c++, I'd have no problem just overloading replaceString//Returns the length of the resulting string (minus the null char)size_t replaceStringLength(const char* from, const char* replace, const char* replacement){size_t fromLength = strlen(from);size_t replaceLength = strlen(replace);size_t replacementLength = strlen(replacement);size_t fromEndIt = 0;size_t toLength = 0;while(fromEndIt < fromLength){int replaceIt = 0;while(replaceIt < replaceLength && (fromEndIt+replaceIt) < fromLength && from[fromEndIt + replaceIt] == replace[replaceIt]){++replaceIt;}if(replaceIt == replaceLength){//Update from buffer iterator positionstoLength += (fromEndIt + replacementLength);from += (fromEndIt + replaceLength);fromEndIt = 0;continue;}++fromEndIt;}toLength += fromEndIt;return toLength;}//Baller replaceString by Leif Messinger//Needs null terminated from, replace, and replacement strings as well as a large block of memory to store the result.void replaceString(char* to, const char* from, const char* replace, const char* replacement){size_t fromLength = strlen(from);size_t replaceLength = strlen(replace);size_t replacementLength = strlen(replacement);size_t fromEndIt = 0;while(fromEndIt < fromLength){int replaceIt = 0;while(replaceIt < replaceLength && (fromEndIt+replaceIt) < fromLength && from[fromEndIt + replaceIt] == replace[replaceIt]){++replaceIt;}if(replaceIt == replaceLength){//Copy the string before the matched bitmemcpyToAndShiftPointers(to, from, fromEndIt);//Copy the replacement toomemcpyToAndShiftPointer(to, replacement, replacementLength);//Update from buffer iterator positionsfrom += replaceLength;fromEndIt = 0;continue; //I don't want this thing to get incremented again}++fromEndIt;}//Copy the rest of the unmatched stringmemcpyToAndShiftPointer(to, from, fromEndIt);to[0] = '\0'; //Should work}//===============replaceString.h===============#ifndef REPLACE_STRING_H#define REPLACE_STRING_H#include <string.h>#define memcpyToAndShiftPointer(to,from,n); memcpy((to),(from),(n)); (to) += (n);#define memcpyToAndShiftPointers(to,from,n); memcpy((to),(from),(n)); (to) += (n); (from) += n;//Normally I'd make a replaceStringMalloc to return a malloced string or a replaceStringMut to change the original string, but I'm not satisfied with those function names so it's best just to do to = (char*)malloc(replaceStringLength(from, replace, replacement)); replaceString(to, from, replace, replacement);//If this was c++, I'd have no problem just overloading replaceString//Returns the length of the resulting string (minus the null char)size_t replaceStringLength(const char* from, const char* replace, const char* replacement);//Baller replaceString by Leif Messinger//Needs null terminated from, replace, and replacement strings as well as a large block of memory to store the result.void replaceString(char* to, const char* from, const char* replace, const char* replacement);#endif//===============replaceStringMain.c===============#include <stdio.h>#include <stdlib.h>#include "replaceString.h"#define BUFFER_SIZE (1<<21) //About a mebibyte//There could be matches between buffers, so make sure the buffer size you set it to is good enough for your application. Basically as big as your input.//Replaces strings from sdtin, then outputs it to stdout//./a.out [[replace], [replacement]]...int main(int argc, char** argv){char* buffer1 = (char*) malloc(BUFFER_SIZE);char* buffer2 = (char*) malloc(BUFFER_SIZE);if(buffer1 == NULL || buffer2 == NULL){perror("You need a couple MBs of ram my boy");return 1;}while(!feof(stdin)){fread(buffer1, BUFFER_SIZE, 1, stdin);char* activeBuffer = buffer1;char* inactiveBuffer = buffer2;char* tmp;for(size_t i = 1; (i + 1) < argc; i += 2){//puts(activeBuffer);replaceString(inactiveBuffer, activeBuffer, argv[i], argv[i+1]);//Swap bufferstmp = activeBuffer;activeBuffer = inactiveBuffer;inactiveBuffer = tmp;}fputs(activeBuffer, stdout);}free(buffer1);free(buffer2);return 0;}
#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 <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>int main() {char * cmd[] = {"who","ls","date"};int i;while (1) {printf("0=who 1=ls 2=date : ");scanf("%d", & i);if (fork() == 0) {/* child */execlp(cmd[i], cmd[i], (char * ) 0);printf("execlp failed\n");exit(1);} else {/* parent */wait((int * ) 0);printf("child finished\n");}} /* while */} /* 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;}