This commit is contained in:
Tien
2019-11-17 15:06:20 +07:00
parent 2ffb7a19a4
commit d753ae2fdc
54 changed files with 899 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
all: ex1.o ex2.o
gcc ex1.o -o ex1 -lgmp
gcc ex2.o -o ex2
ex1.o: ex1.c
gcc -c ex1.c
ex2.o: ex2.c
gcc -c ex2.c
clean:
rm -f *.o ex1 ex2
Binary file not shown.
BIN
View File
Binary file not shown.
+84
View File
@@ -0,0 +1,84 @@
#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#define FILE_NAME "numbers.txt"
#define MAX_SIZE 10
mpz_t array[MAX_SIZE];
int size = 0;
void readfile() {
FILE *file;
char str[80];
mpz_t temp;
mpz_init(temp);
file = fopen(FILE_NAME, "r");
if (file == NULL) {
perror("Can't open file!\n");
exit(1);
}
while (1) {
fgets(str, 80, file);
if (feof(file)) {
break;
}
if (mpz_set_str(temp, str, 10)) {
perror("Error! Not a number");
exit(1);
}
mpz_set(array[size++], temp);
}
mpz_clear(temp);
fclose(file);
}
void count(int n) {
int count = 0;
mpz_t temp;
mpz_init(temp);
for (int i = 0; i < size; ++i) {
mpz_mod_ui(temp, array[i], n);
if (!mpz_cmp_ui(temp, 0)) {
++count;
}
}
printf("Co %d so chia het cho %d.\n", count, n);
mpz_clear(temp);
}
int main() {
int child1_pid = 0,
child2_pid = 0,
child3_pid = 0,
parrent_pid = getpid();
readfile();
child1_pid = fork();
if (child1_pid < 0) {
perror("Error at 1st pork()!\n");
exit(1);
} else if (child1_pid == 0) {
count(2);
}
if (parrent_pid == getpid()) {
child2_pid = fork();
if (child2_pid < 0) {
perror("Error at 2nd pork()!\n");
exit(1);
} else if (child2_pid == 0) {
count(3);
}
}
if (parrent_pid == getpid()) {
child3_pid = fork();
if (child3_pid < 0) {
perror("Error at 3rd pork()!\n");
exit(1);
} else if (child3_pid == 0) {
count(5);
}
}
return 0;
}
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
+9
View File
@@ -0,0 +1,9 @@
12918237493729473291790579217597
4213412542666666666666665
321456765
4432524352352365
106235754869
11356243645879870
345943809680943809584352245
jweois
209345790jdsljflks