feat: Initial commit
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -52,3 +52,5 @@ Module.symvers
|
|||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
|
# Gdb history
|
||||||
|
.gdb_history
|
||||||
5
Lab02/commands.sh
Normal file
5
Lab02/commands.sh
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
touch mioFile.c
|
||||||
|
ls -laR
|
||||||
|
cat mioFile.c
|
||||||
|
mkdir -p tmpDir
|
||||||
|
cp -f mioFile.c tmpDir
|
||||||
30
Lab02/executor.c
Normal file
30
Lab02/executor.c
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#define LEN 128
|
||||||
|
#define USE_LIB 0
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
FILE *flist;
|
||||||
|
|
||||||
|
if (argc < 2) return 1;
|
||||||
|
|
||||||
|
flist = fopen(argv[1], "r");
|
||||||
|
|
||||||
|
char cmd[LEN];
|
||||||
|
|
||||||
|
while(fgets(cmd, LEN, flist)) {
|
||||||
|
if (USE_LIB) {
|
||||||
|
system(cmd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fork()) {
|
||||||
|
execl("/bin/sh", "sh", "-c", cmd, (char *) NULL);
|
||||||
|
} else {
|
||||||
|
sleep(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
4
Lab04/evenFile.txt
Normal file
4
Lab04/evenFile.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
432
|
||||||
|
65346
|
||||||
|
3214
|
||||||
|
856
|
||||||
11
Lab04/inFile.txt
Normal file
11
Lab04/inFile.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
123
|
||||||
|
432
|
||||||
|
75
|
||||||
|
1231231
|
||||||
|
65346
|
||||||
|
3245
|
||||||
|
3214
|
||||||
|
123
|
||||||
|
6345
|
||||||
|
856
|
||||||
|
0
|
||||||
6
Lab04/oddFile.txt
Normal file
6
Lab04/oddFile.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
123
|
||||||
|
75
|
||||||
|
1231231
|
||||||
|
3245
|
||||||
|
123
|
||||||
|
6345
|
||||||
10
Lab04/redir.c
Normal file
10
Lab04/redir.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
//usr/bin/gcc "$0" && ./a.out < inFile.txt 1> evenFile.txt 2> oddFile.txt; rm a.out; exit
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n;
|
||||||
|
while (scanf("%d", &n) != EOF && n != 0) {
|
||||||
|
fprintf((n%2) ? stderr : stdout, "%d\n", n);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
11
Lab05/ex4.sh
Normal file
11
Lab05/ex4.sh
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
sort -r -n infile.txt
|
||||||
|
sort -k 2 infile.txt
|
||||||
|
grep "2011/2012" infile.txt
|
||||||
|
grep "INF1T3" infile.txt | sort -n
|
||||||
|
grep -P "^\d+[24] " infile.txt | sort -k 2
|
||||||
|
grep "TLC1T3" infile.txt | grep "Da frequentare" | sort -n -r
|
||||||
|
grep -P "\<\w+A\w\wA\w+\>" infile.txt
|
||||||
|
cat infile.txt | cut -d " " -f1 | tr -d 0 | tr 8 1 | sort -n | uniq
|
||||||
|
cat infile.txt | cut -d " " -f1 | tr -d 0 | tr 8 1 | sort -n | uniq -d -c
|
||||||
71
Lab05/infile.txt
Normal file
71
Lab05/infile.txt
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
183658 DELL'ANNAX FRANCESCO GIUSEPPE 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186988 YE YU 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186840 BEN AYED AMNA 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
177484 KUMI PRINCE OPOKU 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
173369 WU WENAO 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
89499 DATO AGGIUNTO PER PROVA 1 (10) TLC1T3 1 Da frequentare
|
||||||
|
187747 RAHMAN NAJEEB UR 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
186946 MASOUDI TARAMSARI ZAHRA 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
177076 EL QORAICHI BADR 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
180946 ZEVOLA LUIGI 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186888 LI LONG 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
187028 YU TENGFEI 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
187362 CHEN JIESI 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
177768 LI HUAN 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
94799 DATO AGGIUNTO PER PROVA 2 (10) TLC1T3 1 Da frequentare
|
||||||
|
174780 SAWORO EROMO 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
181872 GALLINA PAOLO 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
174826 HADDADI RASOUL 01OGDLJ (10) TLC1T3 1 Frequentato nel 2011/2012
|
||||||
|
187380 YAN YUE 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
186961 HE MUZHOU 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
176193 LODDO SELENE 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
174107 WANG HAOYI 01OGDLJ (10) TLC1T3 1 Frequentato nel 2011/2012
|
||||||
|
186433 MISTRETTA SERGIO 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
183581 SAADEH TAMER 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
176151 CHEN XIN 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
183534 KHOSHKHOU GILAVAEI VAHID 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187780 HAXHILLARI JOVO 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186955 HAN YIGE 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
180402 BETTONI MARCO 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187245 QIU XIAOLING 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
185443 VARDANEGA ELIA 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
174845 HUANG HAITAO 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
183000 KARIM MERYEM 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
174855 LIU DONG 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
168111 KHATRI NIRANJAN 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187580 CHEN WEI 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187493 HUANG YANYU 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
187609 JAN MAIN MUHAMMAD FAHEEM 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
173809 LIU FENGXIAN 01OGDLJ (10) TLC1T3 1 Frequentato nel 2011/2012
|
||||||
|
176602 JOBARTEH MARIAMA 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
178503 NADEEM MUHAMMAD 01OGDLJ (10) TLC1T3 1 Frequentato nel 2011/2012
|
||||||
|
180412 IACOBUCCI ALEX 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187696 LIN ZIXUAN 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186992 WANG JUNHUI 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
175746 KAFAEI LOTFIE ARASH AHMAD ALESSANDRO 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
158381 KIMANI JAMES MUNYIRI 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
164370 CONG RUI 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
174308 BACCO MICHAEL 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187354 ZENG LUYUAN 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
183886 UR RAHMAN ASAD 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
183917 TARDINI GIOVANNI 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186960 MA QINGYA 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
184130 MA RUI 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
158776 ELLAHI EHSAN 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
186670 WEN SHIYU 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
186959 LI JIAO 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
174789 GE SHENFENG 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187444 SUN SHU 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
174421 CIORNII VITALIE 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
176117 DJAPA PAGNIA SYLVAIN 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186956 YU QIAN 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
173787 KANA ZEBAZE JEAN PIERRE 01OGDLJ (10) TLC1T3 1 Frequentato nel 2011/2012
|
||||||
|
183578 HUSSAIN MURAD 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
187166 LI HAIYAN 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
187041 FORNO EVELINA 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
164690 SHUKAIR WAEL 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
187432 XU GUANGHUAN 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
183548 OBEID RAFIC 01OGDLJ (10) TLC1T3 1 Da frequentare
|
||||||
|
173376 ZHAO LONGSHENG 01OGDLM (10) INF1T3 1 Frequentato nel 2011/2012
|
||||||
|
182304 SIBONA FIORELLA 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
|
186690 STRAZIMIRI ARBER 01OGDLM (10) INF1T3 1 Da frequentare
|
||||||
56
Lab05/pipes.c
Normal file
56
Lab05/pipes.c
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
void receiver(int fd);
|
||||||
|
void sender(int fd);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int fds[2];
|
||||||
|
pipe(fds);
|
||||||
|
if (fork()) {
|
||||||
|
receiver(fds[0]);
|
||||||
|
} else if (fork()) {
|
||||||
|
sender(fds[1]);
|
||||||
|
}
|
||||||
|
wait(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sender(int fd) {
|
||||||
|
char * str;
|
||||||
|
int len;
|
||||||
|
int q = 0;
|
||||||
|
|
||||||
|
while (q != EOF) {
|
||||||
|
q = scanf(" %ms", &str);
|
||||||
|
len = strlen(str);
|
||||||
|
printf("%d\n",len);
|
||||||
|
if (len == 1) break;
|
||||||
|
write(fd, &len, sizeof(int));
|
||||||
|
write(fd, str, len);
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void receiver(int fd) {
|
||||||
|
char * str;
|
||||||
|
int len, q = 0;
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
q = read(fd, &len, sizeof(int));
|
||||||
|
if (q == EOF) break;
|
||||||
|
str = malloc(len * sizeof(char));
|
||||||
|
q = read(fd, str, len);
|
||||||
|
if (q == EOF) break;
|
||||||
|
for (char * c = str; *c != '\0'; c++)
|
||||||
|
*c = toupper(*c);
|
||||||
|
puts(str);
|
||||||
|
free(str);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
85
Lab06/sort.c
Normal file
85
Lab06/sort.c
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <string.h>
|
||||||
|
#define BUF_SIZE 2048
|
||||||
|
|
||||||
|
int **matrix;
|
||||||
|
int lines[100];
|
||||||
|
char filenames[2][10] = {"foo", "bar"};
|
||||||
|
|
||||||
|
int cmpfunc (const void * a, const void * b) {
|
||||||
|
return ( *(int*)a - *(int*)b );
|
||||||
|
}
|
||||||
|
|
||||||
|
int count_lines(FILE* file)
|
||||||
|
{
|
||||||
|
char buf[BUF_SIZE];
|
||||||
|
int counter = 0;
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
size_t res = fread(buf, 1, BUF_SIZE, file);
|
||||||
|
if (ferror(file))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < res; i++)
|
||||||
|
if (buf[i] == '\n')
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
if (feof(file))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *sort(void * vargs) {
|
||||||
|
int num = (int) vargs;
|
||||||
|
|
||||||
|
FILE * fin = fopen(filenames[num], "r");
|
||||||
|
lines[num] = count_lines(fin);
|
||||||
|
fseek(fin,0L,SEEK_SET);
|
||||||
|
matrix[num] = malloc(lines[num]*sizeof(int));
|
||||||
|
for (int i = 0; i < lines[num]; i++) fscanf(fin, "%d", &matrix[num][i]);
|
||||||
|
fclose(fin);
|
||||||
|
|
||||||
|
qsort(matrix[num], lines[num], sizeof(int), cmpfunc);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
pthread_t tids[2];
|
||||||
|
int tmp[1000], res[1000], cur_len;
|
||||||
|
matrix = malloc(2*sizeof(int *));
|
||||||
|
|
||||||
|
pthread_create(&tids[0], NULL, sort, (void *)0);
|
||||||
|
pthread_create(&tids[1], NULL, sort, (void *)1);
|
||||||
|
|
||||||
|
for (int i = 0; i<2; i++) {
|
||||||
|
pthread_join(tids[i], NULL);
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
memcpy(res, matrix[i], sizeof(int)*lines[i]);
|
||||||
|
cur_len = lines[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int a = 0, b = 0, j = 0;
|
||||||
|
while (a<cur_len && b<lines[i]) {
|
||||||
|
tmp[j++] = (res[a] > matrix[i][b]) ? matrix[i][b++] : res[a++];
|
||||||
|
}
|
||||||
|
while (a<cur_len) tmp[j++] = res[a++];
|
||||||
|
while (b<lines[i]) tmp[j++] = matrix[i][b++];
|
||||||
|
cur_len += lines[i];
|
||||||
|
memcpy(res, tmp, sizeof(int)*cur_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i<cur_len;i++) {
|
||||||
|
printf("%d ",res[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
3
Lab07/es1.sh
Normal file
3
Lab07/es1.sh
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash -v
|
||||||
|
|
||||||
|
grep -rHn "$2" $1 | sort -t ":" -k 1,1 -k 2,2rn > $3
|
||||||
9
Lab07/es2.sh
Normal file
9
Lab07/es2.sh
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
for line in $(cat $1)
|
||||||
|
do
|
||||||
|
echo $line | wc -m
|
||||||
|
done | sort -r -n | head -n 1
|
||||||
|
|
||||||
|
wc -l $1
|
||||||
24
Lab07/es4.sh
Normal file
24
Lab07/es4.sh
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
declare -A acr
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]
|
||||||
|
then
|
||||||
|
echo "Missing filename"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
for word in `cat $1`
|
||||||
|
do
|
||||||
|
if [ -v "acr[$word]" ]
|
||||||
|
then
|
||||||
|
acr[$word]=$((acr[$word]+1))
|
||||||
|
else
|
||||||
|
acr[$word]=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
for key in ${!acr[*]}
|
||||||
|
do
|
||||||
|
echo "${key}: ${acr[$key]}"
|
||||||
|
done
|
||||||
22
Lab07/es5.sh
Normal file
22
Lab07/es5.sh
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
finds=0
|
||||||
|
while :
|
||||||
|
do
|
||||||
|
line=$(ps -el | tr -s " " | cut -d " " -f 2,4,14 | grep $1 | cut -d " " -f 1,2)
|
||||||
|
if [[ $(echo $line | cut -d " " -f 1) = "Z" ]]
|
||||||
|
then
|
||||||
|
let finds++
|
||||||
|
if [[ $finds -ge 5 ]]; then
|
||||||
|
pid=$(echo $line | cut -d " " -f 2)
|
||||||
|
kill -9 $pid
|
||||||
|
echo "Killed process $pid"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
elif [[ $finds -ne 0 ]]
|
||||||
|
then
|
||||||
|
finds=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
sleep $2
|
||||||
|
done
|
||||||
3
Lab07/input.txt
Normal file
3
Lab07/input.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
the imagination of nature is far
|
||||||
|
far greater than the imagination
|
||||||
|
of man
|
||||||
28
Lab08/ex1.c
Normal file
28
Lab08/ex1.c
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
int ticketNumber;
|
||||||
|
int turnNumber;
|
||||||
|
|
||||||
|
int atomicIncrement (int *var) {
|
||||||
|
int tmp = *var;
|
||||||
|
*var = tmp + 1;
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init() {
|
||||||
|
ticketNumber = 0;
|
||||||
|
turnNumber = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void lock() {
|
||||||
|
int ticket = atomicIncrement(&ticketNumber);
|
||||||
|
|
||||||
|
while (ticket != turnNumber);
|
||||||
|
|
||||||
|
void unlock() {
|
||||||
|
atomicIncrement(turnNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
80
Lab08/ex2.c
Normal file
80
Lab08/ex2.c
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef int * sem;
|
||||||
|
|
||||||
|
sem sem_init() {
|
||||||
|
sem s = calloc(2, sizeof(int));
|
||||||
|
pipe(s);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sem_lock(sem s) {
|
||||||
|
int tmp;
|
||||||
|
if (read(s[0], &tmp, 1)<0) exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sem_multi_lock(sem s, int n) {
|
||||||
|
for (int i = 0; i < n; i++) sem_lock(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sem_signal(sem s) {
|
||||||
|
if (write(s[1], "0", 1)<0) exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void A(sem sB, sem sC, sem sD) {
|
||||||
|
puts("A");
|
||||||
|
sem_signal(sB);
|
||||||
|
sem_signal(sC);
|
||||||
|
sem_signal(sD);
|
||||||
|
}
|
||||||
|
|
||||||
|
void single_proc(sem x, sem y, int N, char * name) {
|
||||||
|
sem_multi_lock(x, N);
|
||||||
|
puts(name);
|
||||||
|
sem_signal(y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void C(sem sC, sem sE, sem sF) {
|
||||||
|
sem_lock(sC);
|
||||||
|
puts("C");
|
||||||
|
sem_signal(sE);
|
||||||
|
sem_signal(sF);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
sem sA, sB, sC, sD, sE, sF, sG, sH, sI;
|
||||||
|
sA = sem_init();
|
||||||
|
sB = sem_init();
|
||||||
|
sC = sem_init();
|
||||||
|
sD = sem_init();
|
||||||
|
sE = sem_init();
|
||||||
|
sF = sem_init();
|
||||||
|
sG = sem_init();
|
||||||
|
sH = sem_init();
|
||||||
|
sI = sem_init();
|
||||||
|
while(1) {
|
||||||
|
if (!fork()) {
|
||||||
|
A(sB,sC,sD);
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sB, sI, 1, "B");
|
||||||
|
} else if (!fork()) {
|
||||||
|
C(sC, sE, sF);
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sD, sH, 1, "D");
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sE, sG, 1, "E");
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sF, sG, 1, "F");
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sG, sI, 2, "G");
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sH, sI, 1, "H");
|
||||||
|
} else if (!fork()) {
|
||||||
|
single_proc(sI, sA, 3, "I");
|
||||||
|
}
|
||||||
|
sem_lock(sA);
|
||||||
|
sleep(5);
|
||||||
|
}
|
||||||
|
}
|
||||||
57
Lab08/ex4.c
Normal file
57
Lab08/ex4.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
|
||||||
|
sem_t *sems[6];
|
||||||
|
|
||||||
|
typedef struct params {
|
||||||
|
int sPrev;
|
||||||
|
int sNext;
|
||||||
|
int nPrev;
|
||||||
|
int nNext;
|
||||||
|
char * name;
|
||||||
|
} *params_t;
|
||||||
|
|
||||||
|
void* t_code(void * arg) {
|
||||||
|
params_t p = (params_t) arg;
|
||||||
|
while (1) {
|
||||||
|
for (int i = 0; i < p->nPrev; i++) sem_wait(sems[p->sPrev]);
|
||||||
|
puts(p->name);
|
||||||
|
sleep(3);
|
||||||
|
for (int i = 0; i < p->nNext; i++) sem_post(sems[p->sNext]);
|
||||||
|
}
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_t* start_t(int sPrev, int sNext, int nPrev, int nNext, char * name) {
|
||||||
|
pthread_t *t;
|
||||||
|
params_t p = malloc(sizeof(struct params));
|
||||||
|
p->sPrev = sPrev;
|
||||||
|
p->sNext = sNext;
|
||||||
|
p->nPrev = nPrev;
|
||||||
|
p->nNext = nNext;
|
||||||
|
p->name = name;
|
||||||
|
pthread_create(t, NULL, t_code, (void *) p);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
sems[i] = malloc(sizeof(sem_t));
|
||||||
|
sem_init(sems[i], 0, 0);
|
||||||
|
}
|
||||||
|
sem_post(sems[0]);
|
||||||
|
start_t(0, 1, 1, 3, "A");
|
||||||
|
start_t(1, 2, 1, 1, "B");
|
||||||
|
start_t(1, 3, 1, 2, "C");
|
||||||
|
start_t(1, 4, 1, 1, "D");
|
||||||
|
start_t(3, 5, 1, 1, "E");
|
||||||
|
start_t(3, 5, 1, 1, "F");
|
||||||
|
start_t(5, 2, 1, 1, "G");
|
||||||
|
start_t(4, 2, 1, 1, "H");
|
||||||
|
start_t(2, 0, 3, 1, "I");
|
||||||
|
|
||||||
|
sleep(24);
|
||||||
|
}
|
||||||
7
Lab09/a.txt
Normal file
7
Lab09/a.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
1 2 4
|
||||||
|
3 6.3 1
|
||||||
|
2 3 9
|
||||||
|
3 12 5
|
||||||
|
2 1 23
|
||||||
|
3 12 4
|
||||||
|
|
||||||
21
Lab09/conv.c
Normal file
21
Lab09/conv.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
FILE * fin = fopen("a.txt","r");
|
||||||
|
int fout = open("test",O_CREAT|O_WRONLY);
|
||||||
|
|
||||||
|
float c,x;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
while(fscanf(fin, "%f %f %d\n", &c, &x, &n) == 3) {
|
||||||
|
write(fout,&c,sizeof(float));
|
||||||
|
write(fout,&x,sizeof(float));
|
||||||
|
write(fout,&n,sizeof(int));
|
||||||
|
puts("ed uno");
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fin);
|
||||||
|
close(fout);
|
||||||
|
}
|
||||||
79
Lab09/es2.c
Normal file
79
Lab09/es2.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include <pthread.h>
|
||||||
|
#include <semaphore.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#define M_LEN (sizeof(int)+2*sizeof(float))
|
||||||
|
|
||||||
|
sem_t *sR;
|
||||||
|
sem_t *sC[3];
|
||||||
|
sem_t *sM[3];
|
||||||
|
float res[3];
|
||||||
|
|
||||||
|
int line = 0;
|
||||||
|
|
||||||
|
int fin;
|
||||||
|
|
||||||
|
void* compute(void* arg) {
|
||||||
|
int id = *((int *) arg);
|
||||||
|
free(arg);
|
||||||
|
int n;
|
||||||
|
float c, x;
|
||||||
|
while(1) {
|
||||||
|
sem_wait(sM[id]);
|
||||||
|
sem_wait(sR);
|
||||||
|
int r = lseek(fin, (line*3+id)*M_LEN, SEEK_SET);
|
||||||
|
if (r == -1) {
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
read(fin, &c, sizeof(float));
|
||||||
|
read(fin, &x, sizeof(float));
|
||||||
|
read(fin, &n, sizeof(int));
|
||||||
|
printf("%d-%d-%d: %f %f %d\n",id,line,r,c,x,n);
|
||||||
|
sem_post(sR);
|
||||||
|
|
||||||
|
res[id] = c;
|
||||||
|
for (int i = 0; i<n;i++)
|
||||||
|
res[id]*=x;
|
||||||
|
|
||||||
|
sem_post(sC[id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void* merge(void* arg) {
|
||||||
|
while (1) {
|
||||||
|
float sum = 0;
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
sem_wait(sC[i]);
|
||||||
|
sum += res[i];
|
||||||
|
}
|
||||||
|
printf("%f\n", sum);
|
||||||
|
sleep(3);
|
||||||
|
line++;
|
||||||
|
sem_post(sM[0]);
|
||||||
|
sem_post(sM[1]);
|
||||||
|
sem_post(sM[2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
pthread_t t;
|
||||||
|
fin = open(argv[1], O_RDONLY);
|
||||||
|
if (fin == NULL) exit(1);
|
||||||
|
sR = malloc(sizeof(sem_t));
|
||||||
|
sem_init(sR,0,1);
|
||||||
|
for (int i = 0; i<3;i++) {
|
||||||
|
int * arg=malloc(sizeof(int));
|
||||||
|
*arg=i;
|
||||||
|
sC[i] = malloc(sizeof(sem_t));
|
||||||
|
sM[i] = malloc(sizeof(sem_t));
|
||||||
|
sem_init(sC[i],0,0);
|
||||||
|
sem_init(sM[i],0,1);
|
||||||
|
pthread_create(&t,NULL,compute,(void *)arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_create(&t,NULL,merge,NULL);
|
||||||
|
pthread_join(t,NULL);
|
||||||
|
close(fin);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user