signal() example
0 likes • Nov 19, 2022
C
Loading...
More C Posts
#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;}
#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 */}}
//Leif Messingerconst int BUZZER_PIN = 8;const int BUTTON_PIN = 2;const int BUTTON_PINMODE = INPUT_PULLUP;#define DEBOUNCE_DELAY 50class 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 pressedbool 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 timerlastDebounceTime = 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 HIGHif (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 hearshort lag; //Also 30k ms is 30 secondsfloat 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();}
#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);}
#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 <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;}