Skip to main content

inner fork() variables

0 likes • Nov 19, 2022 • 2 views
C
Loading...

More C Posts

Arduino Sound Demo

0 likes • Jan 4, 2022 • 1 view
C
//Leif Messinger
const int BUZZER_PIN = 8;
const int BUTTON_PIN = 2;
const int BUTTON_PINMODE = INPUT_PULLUP;
#define DEBOUNCE_DELAY 50
class Button{
private:
short buttonPin;
bool buttonState;
bool lastButtonState;
int lastDebounceTime;
public:
Button(short buttonPin): buttonState(HIGH), lastButtonState(LOW), lastDebounceTime(millis()){
this->buttonPin = buttonPin;
}
bool checkForPress(const bool desiredState){ //Returns true if button pressed
bool reading = (bool)digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}else if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == desiredState) {
return true;
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
return false;
}
void waitForPress(const bool desiredState){
while(!checkForPress(desiredState));
}
};
class Note{
public:
short frequency; //Short max is ~ 30k, so way higher than you can hear
short lag; //Also 30k ms is 30 seconds
float sustainPercentage;
};
const short normalBeatLength = 500;
const short wholeNote = 4*normalBeatLength;
const short halfNote = 2*normalBeatLength;
const short dottedQuarterNote = normalBeatLength + (normalBeatLength / 2);
const short quarterNote = normalBeatLength;
const short dottedEighthNote = ((3 * normalBeatLength) / 4);
const short eighthNote = normalBeatLength/ 2;
const short sixteenthNote = normalBeatLength / 4;
const float normalSustainLength = .25;
void beep(const Note& note){ //It might go terribly wrong if you try to beep two tones at the same time.
tone(BUZZER_PIN, note.frequency);
delay(note.lag * note.sustainPercentage);
noTone(BUZZER_PIN);
delay(note.lag * (1.0 - note.sustainPercentage));
}
Note song[] = {
{(int)523.25, halfNote, normalSustainLength},
{(int)392, eighthNote, normalSustainLength},
{(int)523.25, quarterNote, normalSustainLength},
{(int)392, dottedEighthNote, normalSustainLength},
{(int)440.00, sixteenthNote, normalSustainLength},
{(int)493.88, quarterNote, normalSustainLength},
{(int)329.63, eighthNote, normalSustainLength},
{(int)329.63, eighthNote, normalSustainLength},
{(int)440.00, quarterNote, normalSustainLength},
{(int)392, dottedEighthNote, normalSustainLength},
{(int)349.23, sixteenthNote, normalSustainLength},
{(int)392, quarterNote, normalSustainLength},
{(int)261.63, dottedEighthNote, normalSustainLength},
{(int)261.63, sixteenthNote, normalSustainLength},
{(int)293.66, quarterNote, normalSustainLength},
{(int)293.66, dottedEighthNote, normalSustainLength},
{(int)329.63, sixteenthNote, normalSustainLength},
{(int)349.23, quarterNote, normalSustainLength},
{(int)349.23, dottedEighthNote, normalSustainLength},
{(int)392.00, sixteenthNote, normalSustainLength},
{(int)440.00, quarterNote, normalSustainLength},
{(int)493.88, eighthNote, normalSustainLength},
{(int)523.25, eighthNote, normalSustainLength},
{(int)587.33, dottedQuarterNote, normalSustainLength},
{(int)392, eighthNote, normalSustainLength},
{(int)659.25, quarterNote, normalSustainLength},
{(int)587.33, dottedEighthNote, normalSustainLength},
{(int)523.25, sixteenthNote, normalSustainLength},
{(int)587.33, quarterNote, normalSustainLength},
{(int)493.88, eighthNote, normalSustainLength},
{(int)392, eighthNote, normalSustainLength},
{(int)523.25, quarterNote, normalSustainLength},
{(int)493.88, dottedEighthNote, normalSustainLength},
{(int)440.00, sixteenthNote, normalSustainLength},
{(int)493.88, quarterNote, normalSustainLength},
{(int)329.63, eighthNote, normalSustainLength},
{(int)329.63, eighthNote, normalSustainLength},
{(int)440.00, quarterNote, normalSustainLength},
{(int)392, dottedEighthNote, normalSustainLength},
{(int)349.23, sixteenthNote, normalSustainLength},
{(int)392, quarterNote, normalSustainLength},
{(int)261.63, dottedEighthNote, normalSustainLength},
{(int)261.63, sixteenthNote, normalSustainLength},
{(int)523.25, quarterNote, normalSustainLength},
{(int)493.88, dottedEighthNote, normalSustainLength},
{(int)440.00, sixteenthNote, normalSustainLength},
{(int)392.00, halfNote, normalSustainLength * .5},
};
size_t songLength = (sizeof(song) / sizeof(Note));
void playSong(){
for(size_t pos = 0; pos < songLength; ++pos){
const Note& bruh = song[pos];
beep(bruh);
}
}
void setup() {
const int BUZZER_PIN = 8;
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, BUTTON_PINMODE);
playSong();
}
Button button(BUTTON_PIN);
void loop() {
button.waitForPress(LOW);
// read the state of the switch into a local variable:
playSong();
}

execlp() example

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

replaceString

0 likes • Nov 14, 2021 • 0 views
C
//===============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 positions
toLength += (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 bit
memcpyToAndShiftPointers(to, from, fromEndIt);
//Copy the replacement too
memcpyToAndShiftPointer(to, replacement, replacementLength);
//Update from buffer iterator positions
from += replaceLength;
fromEndIt = 0;
continue; //I don't want this thing to get incremented again
}
++fromEndIt;
}
//Copy the rest of the unmatched string
memcpyToAndShiftPointer(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 buffers
tmp = activeBuffer;
activeBuffer = inactiveBuffer;
inactiveBuffer = tmp;
}
fputs(activeBuffer, stdout);
}
free(buffer1);
free(buffer2);
return 0;
}

socketTCPClient.c

0 likes • Aug 5, 2023 • 0 views
C
// compile: gcc socketTCPClient.c -o socketTCPClient.out
// usage : ./socketTCPClient.out port
// Doesn't send anything, just connects and prints out the response from the server
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[]){
int sockfd = 0, n = 0, portno;
char recvBuff[1025];
struct sockaddr_in serv_addr;
memset(recvBuff, '0', sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("socket error\n");
exit(EXIT_FAILURE);
}
serv_addr.sin_family = AF_INET;
portno = atoi(argv[1]);
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
printf("connect error\n");
exit(EXIT_FAILURE);
}
while((n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0){
recvBuff[n] = 0;
if (fputs(recvBuff, stdout) == EOF)
{
printf("fputs error\n");
}
}
if(n < 0){
printf("read error\n");
}
return 0;
}

fork() example

0 likes • Nov 19, 2022 • 1 view
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;
}

socketTCPServer.c

0 likes • Aug 5, 2023 • 0 views
C
// compile: gcc socketTCPServer.c -o socketTCPServer.out
// usage : ./socketTCPServer.out port
// Server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(int argc, char *argv[]){
int listenfd = 0, connfd = 0, cli_size, portno;
struct sockaddr_in serv_addr, cli_addr;
char sendBuff[1025];
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
printf("socket error\n");
exit(EXIT_FAILURE);
}
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
portno = atoi(argv[1]);
serv_addr.sin_port = htons(portno);
const int on = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1){
printf("bind error\n");
exit(EXIT_FAILURE);
}
if(listen(listenfd, 5) == -1){
printf("listen error\n");
exit(EXIT_FAILURE);
}
while(1){
cli_size = sizeof(cli_addr);
if((connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &cli_size)) == -1){
printf("accept error\n");
exit(EXIT_FAILURE);
}
strcpy(sendBuff, "Server Message: SUCCESS\n");
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
return 0;
}