Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
/*
* math.h
*
* Created on: Aug 22, 2017
* Last Edited: Sept 5, 2017
* Author: Russell Trafford
*/
/* Your assignment is to take the math function and implement at least the following functions:
* + Add (num1 + num2)
* - Subtract (num1 - num2)
* * Multiply (num1 * num2)
* / Divide (num1 / num2)
* % Modulus (num1 % num2)
*/
#include <stdio.h>
#ifndef MATH_H_
#define MATH_H_
int math(int num1, int num2, char Operator);
int main(){
int num1 = 4;
int num2 = 2;
char Operator = '%';
int ret;
ret = math(num1, num2,Operator);
printf("Answer is : %d\n", ret);
return 0;
}
//Part of your documentation should be listing the valid inputs and outputs for the functions you create.
int math(int num1, int num2, char Operator){
int result;
if (Operator == '+'){
result = num1 + num2;
}
else if (Operator == '-'){
result = num1 - num2;
}
else if (Operator == '*'){
result = num1 * num2;
}
else if (Operator == '/'){
result = num1 / num2;
}
else if (Operator == '%'){
result = num1 % num2;
}
return result;
}
#endif /* MATH_H_ */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int a = 1000;
printf("\nAgain ...%d", (int)getpid());
if (fork()) {
printf("\nIn child ...");
a /= 2;
printf("\nValue of a = %d. PID: %d", a, (int) getpid());
} else {
printf("\nIn parent ...");
if (fork()) {
printf("\nSecond fork() ... PID: %d", (int) getpid());
a*=2;
printf("\nValue of a = %d. PID: %d", a, (int) getpid());
if (execlp("ls","ls", 0) == -1) {
a = a + 2;
printf("\nValue of a = %d. PID: %d", a, (int) getpid());
}
} else {
printf("\nIncrement ...");
a+=2;
printf("\nValue of a = %d. PID: %d", a, (int) getpid());
}
}
a++;
printf("\nValue of a = %d. PID: %d", a, (int) getpid());
printf("\nExiting %d\n", getpid());
return 0;
}