Skip to main content

Arduino Sound Demo

0 likes • Jan 4, 2022
C
Loading...
Download

More C Posts

pipe() example

CodeCatch
0 likes • Nov 19, 2022
C
#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 */
}
}

wait() example

CodeCatch
0 likes • Nov 19, 2022
C
#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 */

Fast inverse square root

CodeCatch
0 likes • Nov 19, 2022
C
// 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;
}

fork() example

CodeCatch
0 likes • Nov 19, 2022
C
#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;
}

execlp() example

CodeCatch
0 likes • Nov 19, 2022
C
#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;
}

inner fork() variables

CodeCatch
0 likes • Nov 19, 2022
C
#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 */