-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuthread.asm
3198 lines (2995 loc) · 111 KB
/
uthread.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_uthread: file format elf32-i386
Disassembly of section .text:
00000000 <run_thread>:
}
void
run_thread(void (*start_func)(void *), void*arg)
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 56 push %esi
4: 53 push %ebx
5: 8b 75 0c mov 0xc(%ebp),%esi
8: 8b 5d 08 mov 0x8(%ebp),%ebx
printf(1, "---- running the function ----\n");
b: 83 ec 08 sub $0x8,%esp
e: 68 9c 11 00 00 push $0x119c
13: 6a 01 push $0x1
15: e8 66 0d 00 00 call d80 <printf>
alarm(UTHREAD_QUANTA);
1a: c7 04 24 05 00 00 00 movl $0x5,(%esp)
21: e8 a4 0c 00 00 call cca <alarm>
start_func(arg);
26: 89 75 08 mov %esi,0x8(%ebp)
29: 83 c4 10 add $0x10,%esp
}
2c: 8d 65 f8 lea -0x8(%ebp),%esp
void
run_thread(void (*start_func)(void *), void*arg)
{
printf(1, "---- running the function ----\n");
alarm(UTHREAD_QUANTA);
start_func(arg);
2f: 89 d8 mov %ebx,%eax
}
31: 5b pop %ebx
32: 5e pop %esi
33: 5d pop %ebp
void
run_thread(void (*start_func)(void *), void*arg)
{
printf(1, "---- running the function ----\n");
alarm(UTHREAD_QUANTA);
start_func(arg);
34: ff e0 jmp *%eax
36: 8d 76 00 lea 0x0(%esi),%esi
39: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
00000040 <uthread_exit>:
return;
}
void
uthread_exit()
{
40: 55 push %ebp
41: 89 e5 mov %esp,%ebp
43: 53 push %ebx
44: 83 ec 10 sub $0x10,%esp
alarm(0); // stopping the alarm until we finish exiting
47: 6a 00 push $0x0
49: e8 7c 0c 00 00 call cca <alarm>
printf(1,"exiting...\n");
4e: 5a pop %edx
4f: 59 pop %ecx
50: 68 0c 13 00 00 push $0x130c
55: 6a 01 push $0x1
57: e8 24 0d 00 00 call d80 <printf>
/* check if i am the main thread*/
if(threads[index_currently_running].thread_id == 0)
5c: a1 28 a8 00 00 mov 0xa828,%eax
61: 83 c4 10 add $0x10,%esp
64: 8d 14 c0 lea (%eax,%eax,8),%edx
67: 8b 1c 95 00 19 00 00 mov 0x1900(,%edx,4),%ebx
6e: 85 db test %ebx,%ebx
70: 75 0a jne 7c <uthread_exit+0x3c>
{
threads[0].state = UNUSED_THREAD;
72: c7 05 08 19 00 00 00 movl $0x0,0x1908
79: 00 00 00
}
/* need to clean the thread field*/
free(threads[index_currently_running].stack);
7c: 8d 04 c0 lea (%eax,%eax,8),%eax
7f: 83 ec 0c sub $0xc,%esp
82: ff 34 85 04 19 00 00 pushl 0x1904(,%eax,4)
89: e8 92 0e 00 00 call f20 <free>
threads[index_currently_running].state = UNUSED_THREAD;
8e: a1 28 a8 00 00 mov 0xa828,%eax
93: 83 c4 10 add $0x10,%esp
96: 8d 14 c0 lea (%eax,%eax,8),%edx
99: b8 08 19 00 00 mov $0x1908,%eax
9e: c1 e2 02 shl $0x2,%edx
a1: c7 82 08 19 00 00 00 movl $0x0,0x1908(%edx)
a8: 00 00 00
threads[index_currently_running].thread_id = -1;
ab: c7 82 00 19 00 00 ff movl $0xffffffff,0x1900(%edx)
b2: ff ff ff
threads[index_currently_running].ebp = 0;
b5: c7 82 0c 19 00 00 00 movl $0x0,0x190c(%edx)
bc: 00 00 00
threads[index_currently_running].esp = 0;
bf: c7 82 10 19 00 00 00 movl $0x0,0x1910(%edx)
c6: 00 00 00
threads[index_currently_running].time_to_get_up = 0;
c9: c7 82 1c 19 00 00 00 movl $0x0,0x191c(%edx)
d0: 00 00 00
threads[index_currently_running].join = -1;
d3: c7 82 20 19 00 00 ff movl $0xffffffff,0x1920(%edx)
da: ff ff ff
dd: eb 0b jmp ea <uthread_exit+0xaa>
df: 90 nop
e0: 83 c0 24 add $0x24,%eax
/* need to find another process to run */
for(int i = 0 ; i < MAX_UTHREADS ; i++){
e3: 3d 08 22 00 00 cmp $0x2208,%eax
e8: 74 15 je ff <uthread_exit+0xbf>
if(threads[i].state == BLOCKED_THREAD) //wake up all blocked threads
ea: 83 38 04 cmpl $0x4,(%eax)
ed: 75 f1 jne e0 <uthread_exit+0xa0>
threads[i].state = RUNNABLE_THREAD;
ef: c7 00 02 00 00 00 movl $0x2,(%eax)
f5: 83 c0 24 add $0x24,%eax
threads[index_currently_running].esp = 0;
threads[index_currently_running].time_to_get_up = 0;
threads[index_currently_running].join = -1;
/* need to find another process to run */
for(int i = 0 ; i < MAX_UTHREADS ; i++){
f8: 3d 08 22 00 00 cmp $0x2208,%eax
fd: 75 eb jne ea <uthread_exit+0xaa>
ff: bb 2c 19 00 00 mov $0x192c,%ebx
104: eb 15 jmp 11b <uthread_exit+0xdb>
106: 8d 76 00 lea 0x0(%esi),%esi
109: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
110: 83 c3 24 add $0x24,%ebx
if(threads[i].state == BLOCKED_THREAD) //wake up all blocked threads
threads[i].state = RUNNABLE_THREAD;
}
for(int i = 1 ; i < MAX_UTHREADS ; i++){
113: 81 fb 08 22 00 00 cmp $0x2208,%ebx
119: 74 24 je 13f <uthread_exit+0xff>
if(threads[i].state != UNUSED_THREAD) // there is more thread to run
11b: 8b 03 mov (%ebx),%eax
11d: 85 c0 test %eax,%eax
11f: 74 ef je 110 <uthread_exit+0xd0>
sigsend(getpid(),14);
121: e8 6c 0b 00 00 call c92 <getpid>
126: 83 ec 08 sub $0x8,%esp
129: 83 c3 24 add $0x24,%ebx
12c: 6a 0e push $0xe
12e: 50 push %eax
12f: e8 86 0b 00 00 call cba <sigsend>
134: 83 c4 10 add $0x10,%esp
for(int i = 0 ; i < MAX_UTHREADS ; i++){
if(threads[i].state == BLOCKED_THREAD) //wake up all blocked threads
threads[i].state = RUNNABLE_THREAD;
}
for(int i = 1 ; i < MAX_UTHREADS ; i++){
137: 81 fb 08 22 00 00 cmp $0x2208,%ebx
13d: 75 dc jne 11b <uthread_exit+0xdb>
if(threads[i].state != UNUSED_THREAD) // there is more thread to run
sigsend(getpid(),14);
}
exit();
13f: e8 ce 0a 00 00 call c12 <exit>
144: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
14a: 8d bf 00 00 00 00 lea 0x0(%edi),%edi
00000150 <init_bsem>:
struct binarySemaphore binSemaphore[MAX_BSEM];
int binSemCounter = 1 ; //to give id's to semaphores like 'id_number_thread' ;
int first_run_bsem = 0; // if 0 then first run
void
init_bsem()
{
150: 55 push %ebp
151: ba 28 23 00 00 mov $0x2328,%edx
156: 89 e5 mov %esp,%ebp
158: 90 nop
159: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
160: 8d 82 00 ff ff ff lea -0x100(%edx),%eax
for(int i = 0; i <MAX_BSEM; i++) {
binSemaphore[i].state = UNUSED;
166: c7 02 01 00 00 00 movl $0x1,(%edx)
16c: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
for (int j=0;j<MAX_UTHREADS;j++){
binSemaphore[i].sleep[j] = -1;
170: c7 00 ff ff ff ff movl $0xffffffff,(%eax)
176: 83 c0 04 add $0x4,%eax
void
init_bsem()
{
for(int i = 0; i <MAX_BSEM; i++) {
binSemaphore[i].state = UNUSED;
for (int j=0;j<MAX_UTHREADS;j++){
179: 39 d0 cmp %edx,%eax
17b: 75 f3 jne 170 <init_bsem+0x20>
17d: 8d 90 0c 01 00 00 lea 0x10c(%eax),%edx
int binSemCounter = 1 ; //to give id's to semaphores like 'id_number_thread' ;
int first_run_bsem = 0; // if 0 then first run
void
init_bsem()
{
for(int i = 0; i <MAX_BSEM; i++) {
183: 81 fa 28 a9 00 00 cmp $0xa928,%edx
189: 75 d5 jne 160 <init_bsem+0x10>
for (int j=0;j<MAX_UTHREADS;j++){
binSemaphore[i].sleep[j] = -1;
}
}
//printf(1,"done init binary semaphore!\n");
first_run_bsem=1;
18b: c7 05 e0 18 00 00 01 movl $0x1,0x18e0
192: 00 00 00
}
195: 5d pop %ebp
196: c3 ret
197: 89 f6 mov %esi,%esi
199: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
000001a0 <bsem_alloc>:
int bsem_alloc() {
1a0: 55 push %ebp
1a1: 89 e5 mov %esp,%ebp
1a3: 53 push %ebx
1a4: 83 ec 10 sub $0x10,%esp
alarm(0);
1a7: 6a 00 push $0x0
1a9: e8 1c 0b 00 00 call cca <alarm>
int i;
if(first_run_bsem == 0)
1ae: a1 e0 18 00 00 mov 0x18e0,%eax
1b3: 83 c4 10 add $0x10,%esp
1b6: 85 c0 test %eax,%eax
1b8: 75 3b jne 1f5 <bsem_alloc+0x55>
1ba: ba 28 23 00 00 mov $0x2328,%edx
1bf: 90 nop
1c0: 8d 82 00 ff ff ff lea -0x100(%edx),%eax
int first_run_bsem = 0; // if 0 then first run
void
init_bsem()
{
for(int i = 0; i <MAX_BSEM; i++) {
binSemaphore[i].state = UNUSED;
1c6: c7 02 01 00 00 00 movl $0x1,(%edx)
1cc: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
for (int j=0;j<MAX_UTHREADS;j++){
binSemaphore[i].sleep[j] = -1;
1d0: c7 00 ff ff ff ff movl $0xffffffff,(%eax)
1d6: 83 c0 04 add $0x4,%eax
void
init_bsem()
{
for(int i = 0; i <MAX_BSEM; i++) {
binSemaphore[i].state = UNUSED;
for (int j=0;j<MAX_UTHREADS;j++){
1d9: 39 d0 cmp %edx,%eax
1db: 75 f3 jne 1d0 <bsem_alloc+0x30>
1dd: 8d 90 0c 01 00 00 lea 0x10c(%eax),%edx
int binSemCounter = 1 ; //to give id's to semaphores like 'id_number_thread' ;
int first_run_bsem = 0; // if 0 then first run
void
init_bsem()
{
for(int i = 0; i <MAX_BSEM; i++) {
1e3: 81 fa 28 a9 00 00 cmp $0xa928,%edx
1e9: 75 d5 jne 1c0 <bsem_alloc+0x20>
for (int j=0;j<MAX_UTHREADS;j++){
binSemaphore[i].sleep[j] = -1;
}
}
//printf(1,"done init binary semaphore!\n");
first_run_bsem=1;
1eb: c7 05 e0 18 00 00 01 movl $0x1,0x18e0
1f2: 00 00 00
1f5: ba 28 23 00 00 mov $0x2328,%edx
if(first_run_bsem == 0)
{
init_bsem();
}
//search for unused semaphore
for(i = 0; i <MAX_BSEM; i++) {
1fa: 31 c0 xor %eax,%eax
1fc: eb 10 jmp 20e <bsem_alloc+0x6e>
1fe: 83 c0 01 add $0x1,%eax
201: 81 c2 0c 01 00 00 add $0x10c,%edx
207: 3d 80 00 00 00 cmp $0x80,%eax
20c: 74 4f je 25d <bsem_alloc+0xbd>
if(binSemaphore[i].state == UNUSED)
20e: 83 3a 01 cmpl $0x1,(%edx)
211: 75 eb jne 1fe <bsem_alloc+0x5e>
break;
}
if (i<MAX_BSEM){
binSemaphore[i].id = binSemCounter;
213: 69 c0 0c 01 00 00 imul $0x10c,%eax,%eax
219: 8b 15 d8 18 00 00 mov 0x18d8,%edx
binSemCounter++;
binSemaphore[i].lock = 1; // not locked
binSemaphore[i].state = USED;
// printf(1,"initialize semaphore in index %d, with id %d\n",i,binSemCounter-1);
alarm(UTHREAD_QUANTA);
21f: 83 ec 0c sub $0xc,%esp
222: 6a 05 push $0x5
for(i = 0; i <MAX_BSEM; i++) {
if(binSemaphore[i].state == UNUSED)
break;
}
if (i<MAX_BSEM){
binSemaphore[i].id = binSemCounter;
224: 8d 98 20 22 00 00 lea 0x2220(%eax),%ebx
22a: 89 90 24 22 00 00 mov %edx,0x2224(%eax)
binSemCounter++;
230: 83 c2 01 add $0x1,%edx
233: 89 15 d8 18 00 00 mov %edx,0x18d8
binSemaphore[i].lock = 1; // not locked
239: c7 80 20 22 00 00 01 movl $0x1,0x2220(%eax)
240: 00 00 00
binSemaphore[i].state = USED;
243: c7 80 28 23 00 00 00 movl $0x0,0x2328(%eax)
24a: 00 00 00
// printf(1,"initialize semaphore in index %d, with id %d\n",i,binSemCounter-1);
alarm(UTHREAD_QUANTA);
24d: e8 78 0a 00 00 call cca <alarm>
return binSemaphore[i].id;
252: 8b 43 04 mov 0x4(%ebx),%eax
255: 83 c4 10 add $0x10,%esp
else{ //all semaphores are used
printf(1,"all semaphores are being used\n");
alarm(UTHREAD_QUANTA);
return -1;
}
}
258: 8b 5d fc mov -0x4(%ebp),%ebx
25b: c9 leave
25c: c3 ret
// printf(1,"initialize semaphore in index %d, with id %d\n",i,binSemCounter-1);
alarm(UTHREAD_QUANTA);
return binSemaphore[i].id;
}
else{ //all semaphores are used
printf(1,"all semaphores are being used\n");
25d: 83 ec 08 sub $0x8,%esp
260: 68 bc 11 00 00 push $0x11bc
265: 6a 01 push $0x1
267: e8 14 0b 00 00 call d80 <printf>
alarm(UTHREAD_QUANTA);
26c: c7 04 24 05 00 00 00 movl $0x5,(%esp)
273: e8 52 0a 00 00 call cca <alarm>
return -1;
278: 83 c4 10 add $0x10,%esp
27b: b8 ff ff ff ff mov $0xffffffff,%eax
}
}
280: 8b 5d fc mov -0x4(%ebp),%ebx
283: c9 leave
284: c3 ret
285: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
289: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
00000290 <bsem_free>:
void bsem_free(int id) {
290: 55 push %ebp
291: 89 e5 mov %esp,%ebp
293: 53 push %ebx
294: 83 ec 10 sub $0x10,%esp
297: 8b 5d 08 mov 0x8(%ebp),%ebx
alarm(0);
29a: 6a 00 push $0x0
29c: e8 29 0a 00 00 call cca <alarm>
2a1: ba 24 22 00 00 mov $0x2224,%edx
2a6: 83 c4 10 add $0x10,%esp
int i;
/* search for the Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
2a9: 31 c0 xor %eax,%eax
2ab: eb 13 jmp 2c0 <bsem_free+0x30>
2ad: 8d 76 00 lea 0x0(%esi),%esi
2b0: 83 c0 01 add $0x1,%eax
2b3: 81 c2 0c 01 00 00 add $0x10c,%edx
2b9: 3d 80 00 00 00 cmp $0x80,%eax
2be: 74 38 je 2f8 <bsem_free+0x68>
if(binSemaphore[i].id == id) {
2c0: 39 1a cmp %ebx,(%edx)
2c2: 75 ec jne 2b0 <bsem_free+0x20>
if (binSemaphore[i].state == UNUSED) {
2c4: 69 c0 0c 01 00 00 imul $0x10c,%eax,%eax
2ca: 05 20 22 00 00 add $0x2220,%eax
2cf: 83 b8 08 01 00 00 01 cmpl $0x1,0x108(%eax)
2d6: 74 0a je 2e2 <bsem_free+0x52>
break;
}
}
/* Semaphore is found*/
if (i<MAX_BSEM){
binSemaphore[i].state = UNUSED;
2d8: c7 80 08 01 00 00 01 movl $0x1,0x108(%eax)
2df: 00 00 00
/* search for the Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
if(binSemaphore[i].id == id) {
if (binSemaphore[i].state == UNUSED) {
// printf(1,"binary semaphore in index %d, with id %d is already unused!\n",i,id);
alarm(UTHREAD_QUANTA);
2e2: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
// printf(1,"binary semaphore with index %d with id %d is now unused \n",i, id);
}
else
printf(1,"can't find id %d\n",id);
alarm(UTHREAD_QUANTA);
}
2e9: 8b 5d fc mov -0x4(%ebp),%ebx
2ec: c9 leave
/* search for the Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
if(binSemaphore[i].id == id) {
if (binSemaphore[i].state == UNUSED) {
// printf(1,"binary semaphore in index %d, with id %d is already unused!\n",i,id);
alarm(UTHREAD_QUANTA);
2ed: e9 d8 09 00 00 jmp cca <alarm>
2f2: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
if (i<MAX_BSEM){
binSemaphore[i].state = UNUSED;
// printf(1,"binary semaphore with index %d with id %d is now unused \n",i, id);
}
else
printf(1,"can't find id %d\n",id);
2f8: 83 ec 04 sub $0x4,%esp
2fb: 53 push %ebx
2fc: 68 18 13 00 00 push $0x1318
301: 6a 01 push $0x1
303: e8 78 0a 00 00 call d80 <printf>
308: 83 c4 10 add $0x10,%esp
/* search for the Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
if(binSemaphore[i].id == id) {
if (binSemaphore[i].state == UNUSED) {
// printf(1,"binary semaphore in index %d, with id %d is already unused!\n",i,id);
alarm(UTHREAD_QUANTA);
30b: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
// printf(1,"binary semaphore with index %d with id %d is now unused \n",i, id);
}
else
printf(1,"can't find id %d\n",id);
alarm(UTHREAD_QUANTA);
}
312: 8b 5d fc mov -0x4(%ebp),%ebx
315: c9 leave
/* search for the Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
if(binSemaphore[i].id == id) {
if (binSemaphore[i].state == UNUSED) {
// printf(1,"binary semaphore in index %d, with id %d is already unused!\n",i,id);
alarm(UTHREAD_QUANTA);
316: e9 af 09 00 00 jmp cca <alarm>
31b: 90 nop
31c: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
00000320 <bsem_down>:
printf(1,"can't find id %d\n",id);
alarm(UTHREAD_QUANTA);
}
void bsem_down(int id) {
320: 55 push %ebp
321: 89 e5 mov %esp,%ebp
323: 53 push %ebx
324: 83 ec 10 sub $0x10,%esp
327: 8b 5d 08 mov 0x8(%ebp),%ebx
alarm(0);
32a: 6a 00 push $0x0
32c: e8 99 09 00 00 call cca <alarm>
331: b8 24 22 00 00 mov $0x2224,%eax
336: 83 c4 10 add $0x10,%esp
int i;
/* searching for Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
339: 31 d2 xor %edx,%edx
33b: eb 17 jmp 354 <bsem_down+0x34>
33d: 8d 76 00 lea 0x0(%esi),%esi
340: 83 c2 01 add $0x1,%edx
343: 05 0c 01 00 00 add $0x10c,%eax
348: 81 fa 80 00 00 00 cmp $0x80,%edx
34e: 0f 84 8c 00 00 00 je 3e0 <bsem_down+0xc0>
if(binSemaphore[i].id == id && binSemaphore[i].state == USED)
354: 39 18 cmp %ebx,(%eax)
356: 75 e8 jne 340 <bsem_down+0x20>
358: 8b 88 04 01 00 00 mov 0x104(%eax),%ecx
35e: 85 c9 test %ecx,%ecx
360: 75 de jne 340 <bsem_down+0x20>
break;
}
/* found */
if (i<MAX_BSEM){
if(binSemaphore[i].lock == 1) { //if lock not taken
362: 69 ca 0c 01 00 00 imul $0x10c,%edx,%ecx
368: 31 c0 xor %eax,%eax
36a: 83 b9 20 22 00 00 01 cmpl $0x1,0x2220(%ecx)
371: 75 15 jne 388 <bsem_down+0x68>
373: e9 93 00 00 00 jmp 40b <bsem_down+0xeb>
378: 90 nop
379: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
// printf(1, "thread %d got lock for binary semaphore with id %d \n",threads[index_currently_running].thread_id, id);
binSemaphore[i].lock=0;
}
else{ // can't take lock,need to sleep on semaphore
// printf(1, "lock is already taken - thread %d going to sleep on binary semaphore with id %d\n",threads[index_currently_running].thread_id, id);
for (int j=0;j<MAX_UTHREADS;j++){
380: 83 c0 01 add $0x1,%eax
383: 83 f8 40 cmp $0x40,%eax
386: 74 7b je 403 <bsem_down+0xe3>
if (binSemaphore[i].sleep[j] == -1){
388: 83 bc 81 28 22 00 00 cmpl $0xffffffff,0x2228(%ecx,%eax,4)
38f: ff
390: 75 ee jne 380 <bsem_down+0x60>
binSemaphore[i].sleep[j] = threads[index_currently_running].thread_id;
392: 8b 0d 28 a8 00 00 mov 0xa828,%ecx
398: 6b d2 43 imul $0x43,%edx,%edx
39b: 01 c2 add %eax,%edx
39d: 8d 04 c9 lea (%ecx,%ecx,8),%eax
3a0: 8b 04 85 00 19 00 00 mov 0x1900(,%eax,4),%eax
3a7: 89 04 95 28 22 00 00 mov %eax,0x2228(,%edx,4)
break;
}
}
// printf(1,"zZzzZzzzZzzz.....\n");
threads[index_currently_running].state = BLOCKED_THREAD;
3ae: 8d 04 c9 lea (%ecx,%ecx,8),%eax
3b1: c7 04 85 08 19 00 00 movl $0x4,0x1908(,%eax,4)
3b8: 04 00 00 00
sigsend(getpid(),14);
3bc: e8 d1 08 00 00 call c92 <getpid>
3c1: 83 ec 08 sub $0x8,%esp
3c4: 6a 0e push $0xe
3c6: 50 push %eax
3c7: e8 ee 08 00 00 call cba <sigsend>
3cc: 83 c4 10 add $0x10,%esp
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
3cf: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
}
3d6: 8b 5d fc mov -0x4(%ebp),%ebx
3d9: c9 leave
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
3da: e9 eb 08 00 00 jmp cca <alarm>
3df: 90 nop
sigsend(getpid(),14);
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
3e0: 83 ec 04 sub $0x4,%esp
3e3: 53 push %ebx
3e4: 68 dc 11 00 00 push $0x11dc
3e9: 6a 01 push $0x1
3eb: e8 90 09 00 00 call d80 <printf>
3f0: 83 c4 10 add $0x10,%esp
alarm(UTHREAD_QUANTA);
3f3: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
}
3fa: 8b 5d fc mov -0x4(%ebp),%ebx
3fd: c9 leave
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
3fe: e9 c7 08 00 00 jmp cca <alarm>
403: 8b 0d 28 a8 00 00 mov 0xa828,%ecx
409: eb a3 jmp 3ae <bsem_down+0x8e>
}
/* found */
if (i<MAX_BSEM){
if(binSemaphore[i].lock == 1) { //if lock not taken
// printf(1, "thread %d got lock for binary semaphore with id %d \n",threads[index_currently_running].thread_id, id);
binSemaphore[i].lock=0;
40b: c7 81 20 22 00 00 00 movl $0x0,0x2220(%ecx)
412: 00 00 00
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
415: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
}
41c: 8b 5d fc mov -0x4(%ebp),%ebx
41f: c9 leave
}
}
else
printf(1,"you tried to lock a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
420: e9 a5 08 00 00 jmp cca <alarm>
425: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
429: 8d bc 27 00 00 00 00 lea 0x0(%edi,%eiz,1),%edi
00000430 <bsem_up>:
}
void bsem_up(int id) {
430: 55 push %ebp
431: 89 e5 mov %esp,%ebp
433: 57 push %edi
434: 56 push %esi
435: 53 push %ebx
436: 83 ec 28 sub $0x28,%esp
439: 8b 5d 08 mov 0x8(%ebp),%ebx
alarm(0);
43c: 6a 00 push $0x0
43e: e8 87 08 00 00 call cca <alarm>
443: ba 24 22 00 00 mov $0x2224,%edx
448: 83 c4 10 add $0x10,%esp
int i;
/* searching for Semaphore */
for(i = 0; i <MAX_BSEM; i++) {
44b: 31 c0 xor %eax,%eax
44d: eb 15 jmp 464 <bsem_up+0x34>
44f: 90 nop
450: 83 c0 01 add $0x1,%eax
453: 81 c2 0c 01 00 00 add $0x10c,%edx
459: 3d 80 00 00 00 cmp $0x80,%eax
45e: 0f 84 ac 00 00 00 je 510 <bsem_up+0xe0>
if(binSemaphore[i].id == id && binSemaphore[i].state == USED)
464: 39 1a cmp %ebx,(%edx)
466: 75 e8 jne 450 <bsem_up+0x20>
468: 8b 8a 04 01 00 00 mov 0x104(%edx),%ecx
46e: 85 c9 test %ecx,%ecx
470: 75 de jne 450 <bsem_up+0x20>
break;
}
}
/* found */
if (i<MAX_BSEM){
if(binSemaphore[i].lock == 0) { //lock is taken
472: 69 f8 0c 01 00 00 imul $0x10c,%eax,%edi
478: 8b 97 20 22 00 00 mov 0x2220(%edi),%edx
47e: 89 7d e4 mov %edi,-0x1c(%ebp)
481: 85 d2 test %edx,%edx
483: 0f 85 9c 00 00 00 jne 525 <bsem_up+0xf5>
489: 31 ff xor %edi,%edi
// printf(1, "thread %d want to free semaphore with id %d \n",threads[index_currently_running].thread_id, id);
int j;
for (j=0;j<MAX_UTHREADS;j++){
if (binSemaphore[i].sleep[j] != -1){
48b: 8b 75 e4 mov -0x1c(%ebp),%esi
48e: 8b b4 be 28 22 00 00 mov 0x2228(%esi,%edi,4),%esi
495: 83 fe ff cmp $0xffffffff,%esi
498: 74 56 je 4f0 <bsem_up+0xc0>
49a: b9 00 19 00 00 mov $0x1900,%ecx
49f: 31 d2 xor %edx,%edx
4a1: eb 10 jmp 4b3 <bsem_up+0x83>
4a3: 90 nop
4a4: 8d 74 26 00 lea 0x0(%esi,%eiz,1),%esi
//found a blocked thread
for (int h=0;h<MAX_UTHREADS;h++){
4a8: 83 c2 01 add $0x1,%edx
4ab: 83 c1 24 add $0x24,%ecx
4ae: 83 fa 40 cmp $0x40,%edx
4b1: 74 3d je 4f0 <bsem_up+0xc0>
//search for index of thread with id to give him the lock
if (threads[h].thread_id != -1 && threads[h].thread_id == binSemaphore[i].sleep[j]){
4b3: 8b 19 mov (%ecx),%ebx
4b5: 39 de cmp %ebx,%esi
4b7: 75 ef jne 4a8 <bsem_up+0x78>
4b9: 83 fb ff cmp $0xffffffff,%ebx
4bc: 74 ea je 4a8 <bsem_up+0x78>
threads[h].state=RUNNABLE_THREAD;
binSemaphore[i].sleep[j] =- 1;
4be: 6b c0 43 imul $0x43,%eax,%eax
//found a blocked thread
for (int h=0;h<MAX_UTHREADS;h++){
//search for index of thread with id to give him the lock
if (threads[h].thread_id != -1 && threads[h].thread_id == binSemaphore[i].sleep[j]){
threads[h].state=RUNNABLE_THREAD;
4c1: 8d 14 d2 lea (%edx,%edx,8),%edx
4c4: c7 04 95 08 19 00 00 movl $0x2,0x1908(,%edx,4)
4cb: 02 00 00 00
binSemaphore[i].sleep[j] =- 1;
4cf: 01 c7 add %eax,%edi
4d1: c7 04 bd 28 22 00 00 movl $0xffffffff,0x2228(,%edi,4)
4d8: ff ff ff ff
}
}
/* didnt found */
else
printf(1,"you tried to free a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
4dc: c7 45 08 05 00 00 00 movl $0x5,0x8(%ebp)
}
4e3: 8d 65 f4 lea -0xc(%ebp),%esp
4e6: 5b pop %ebx
4e7: 5e pop %esi
4e8: 5f pop %edi
4e9: 5d pop %ebp
}
}
/* didnt found */
else
printf(1,"you tried to free a binary semaphore that does not exist! (id: %d)\n", id);
alarm(UTHREAD_QUANTA);
4ea: e9 db 07 00 00 jmp cca <alarm>
4ef: 90 nop
/* found */
if (i<MAX_BSEM){
if(binSemaphore[i].lock == 0) { //lock is taken
// printf(1, "thread %d want to free semaphore with id %d \n",threads[index_currently_running].thread_id, id);
int j;
for (j=0;j<MAX_UTHREADS;j++){
4f0: 83 c7 01 add $0x1,%edi
4f3: 83 ff 40 cmp $0x40,%edi
4f6: 75 93 jne 48b <bsem_up+0x5b>
}
cont:
if(j == MAX_UTHREADS){
// there is no blocked thread
// printf(1,"semaphore %d is now free!\n",id);
binSemaphore[i].lock = 1;
4f8: 69 c0 0c 01 00 00 imul $0x10c,%eax,%eax
4fe: c7 80 20 22 00 00 01 movl $0x1,0x2220(%eax)
505: 00 00 00
508: eb d2 jmp 4dc <bsem_up+0xac>
50a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
printf(1, "lock is free - wasted operation\n" );
}
}
/* didnt found */
else
printf(1,"you tried to free a binary semaphore that does not exist! (id: %d)\n", id);
510: 83 ec 04 sub $0x4,%esp
513: 53 push %ebx
514: 68 44 12 00 00 push $0x1244
519: 6a 01 push $0x1
51b: e8 60 08 00 00 call d80 <printf>
520: 83 c4 10 add $0x10,%esp
523: eb b7 jmp 4dc <bsem_up+0xac>
// printf(1,"semaphore %d is now free!\n",id);
binSemaphore[i].lock = 1;
}
}
else{ // lock is free
printf(1, "lock is free - wasted operation\n" );
525: 83 ec 08 sub $0x8,%esp
528: 68 20 12 00 00 push $0x1220
52d: 6a 01 push $0x1
52f: e8 4c 08 00 00 call d80 <printf>
534: 83 c4 10 add $0x10,%esp
537: eb a3 jmp 4dc <bsem_up+0xac>
539: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
00000540 <uthread_init>:
start_func(arg);
}
int
uthread_init()
{
540: 55 push %ebp
541: 89 e5 mov %esp,%ebp
543: 83 ec 10 sub $0x10,%esp
printf(1, "-------initializing the threads -------\n");
546: 68 88 12 00 00 push $0x1288
54b: 6a 01 push $0x1
54d: e8 2e 08 00 00 call d80 <printf>
552: b8 00 19 00 00 mov $0x1900,%eax
557: 83 c4 10 add $0x10,%esp
55a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
// Initializes the process’ threads table
for(int i = 0 ; i < MAX_UTHREADS ; i++)
{
threads[i].thread_id = -1; // if the thread is unused there is no id
560: c7 00 ff ff ff ff movl $0xffffffff,(%eax)
threads[i].state = UNUSED_THREAD;
566: c7 40 08 00 00 00 00 movl $0x0,0x8(%eax)
56d: 83 c0 24 add $0x24,%eax
threads[i].ebp = 0;
570: c7 40 e8 00 00 00 00 movl $0x0,-0x18(%eax)
threads[i].esp = 0;
577: c7 40 ec 00 00 00 00 movl $0x0,-0x14(%eax)
threads[i].time_to_get_up = 0;
57e: c7 40 f8 00 00 00 00 movl $0x0,-0x8(%eax)
threads[i].join=-1;
585: c7 40 fc ff ff ff ff movl $0xffffffff,-0x4(%eax)
uthread_init()
{
printf(1, "-------initializing the threads -------\n");
// Initializes the process’ threads table
for(int i = 0 ; i < MAX_UTHREADS ; i++)
58c: 3d 00 22 00 00 cmp $0x2200,%eax
591: 75 cd jne 560 <uthread_init+0x20>
id_number_thread = 1;
index_currently_running = 0;
currently_running = 0;
// Registers the SIGALRM handler to the uthread_schedule function
signal(14, (sighandler_t)(uthread_schedule));
593: 83 ec 08 sub $0x8,%esp
threads[i].esp = 0;
threads[i].time_to_get_up = 0;
threads[i].join=-1;
}
// Creates the main thread
threads[0].thread_id = 0;
596: c7 05 00 19 00 00 00 movl $0x0,0x1900
59d: 00 00 00
threads[0].state = RUNNING_THREAD;
5a0: c7 05 08 19 00 00 03 movl $0x3,0x1908
5a7: 00 00 00
id_number_thread = 1;
index_currently_running = 0;
currently_running = 0;
// Registers the SIGALRM handler to the uthread_schedule function
signal(14, (sighandler_t)(uthread_schedule));
5aa: 68 40 07 00 00 push $0x740
5af: 6a 0e push $0xe
threads[i].join=-1;
}
// Creates the main thread
threads[0].thread_id = 0;
threads[0].state = RUNNING_THREAD;
threads[0].stack = 0;
5b1: c7 05 04 19 00 00 00 movl $0x0,0x1904
5b8: 00 00 00
id_number_thread = 1;
5bb: c7 05 24 a8 00 00 01 movl $0x1,0xa824
5c2: 00 00 00
index_currently_running = 0;
5c5: c7 05 28 a8 00 00 00 movl $0x0,0xa828
5cc: 00 00 00
currently_running = 0;
5cf: c7 05 00 22 00 00 00 movl $0x0,0x2200
5d6: 00 00 00
// Registers the SIGALRM handler to the uthread_schedule function
signal(14, (sighandler_t)(uthread_schedule));
5d9: e8 d4 06 00 00 call cb2 <signal>
// Executes the alarm system call with parameter UTHREAD_QUANTA=5
alarm(UTHREAD_QUANTA);
5de: c7 04 24 05 00 00 00 movl $0x5,(%esp)
5e5: e8 e0 06 00 00 call cca <alarm>
return 0;
}
5ea: 31 c0 xor %eax,%eax
5ec: c9 leave
5ed: c3 ret
5ee: 66 90 xchg %ax,%ax
000005f0 <uthread_create>:
int
uthread_create(void (*start_func)(void *), void*arg)
{
5f0: 55 push %ebp
5f1: 89 e5 mov %esp,%ebp
5f3: 56 push %esi
5f4: 53 push %ebx
alarm(0);
5f5: 83 ec 0c sub $0xc,%esp
5f8: 6a 00 push $0x0
5fa: e8 cb 06 00 00 call cca <alarm>
printf(1, "------ creating a new thread ------\n");
5ff: 5b pop %ebx
600: 5e pop %esi
601: 68 b4 12 00 00 push $0x12b4
606: 6a 01 push $0x1
608: e8 73 07 00 00 call d80 <printf>
60d: ba 08 19 00 00 mov $0x1908,%edx
612: 83 c4 10 add $0x10,%esp
/* serching for an empty process*/
int free_thread = -1;
for(int i = 0; i < MAX_UTHREADS ; i++)
615: 31 c0 xor %eax,%eax
617: eb 12 jmp 62b <uthread_create+0x3b>
619: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
620: 83 c0 01 add $0x1,%eax
623: 83 c2 24 add $0x24,%edx
626: 83 f8 40 cmp $0x40,%eax
629: 74 7d je 6a8 <uthread_create+0xb8>
{
if(threads[i].state == UNUSED_THREAD)
62b: 8b 0a mov (%edx),%ecx
62d: 85 c9 test %ecx,%ecx
62f: 75 ef jne 620 <uthread_create+0x30>
printf(1, "there is no available thread\n");
alarm(UTHREAD_QUANTA);
return -1;
}
/* naming the thread */
threads[free_thread].thread_id = id_number_thread;
631: 8b 15 24 a8 00 00 mov 0xa824,%edx
637: 8d 1c c0 lea (%eax,%eax,8),%ebx
id_number_thread++;
/* creating the stack */
threads[free_thread].stack = malloc(UTHREAD_STACK_SIZE);
63a: 83 ec 0c sub $0xc,%esp
63d: 68 00 10 00 00 push $0x1000
printf(1, "there is no available thread\n");
alarm(UTHREAD_QUANTA);
return -1;
}
/* naming the thread */
threads[free_thread].thread_id = id_number_thread;
642: c1 e3 02 shl $0x2,%ebx
645: 89 93 00 19 00 00 mov %edx,0x1900(%ebx)
id_number_thread++;
64b: 83 c2 01 add $0x1,%edx
64e: 89 15 24 a8 00 00 mov %edx,0xa824
/* creating the stack */
threads[free_thread].stack = malloc(UTHREAD_STACK_SIZE);
654: e8 57 09 00 00 call fb0 <malloc>
/* initializing the stack */
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 3*sizeof(int))) = (int)uthread_exit;
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 2*sizeof(int))) = (int)start_func;
659: 8b 55 08 mov 0x8(%ebp),%edx
/* creating the stack */
threads[free_thread].stack = malloc(UTHREAD_STACK_SIZE);
/* initializing the stack */
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 3*sizeof(int))) = (int)uthread_exit;
65c: c7 80 f4 0f 00 00 40 movl $0x40,0xff4(%eax)
663: 00 00 00
/* naming the thread */
threads[free_thread].thread_id = id_number_thread;
id_number_thread++;
/* creating the stack */
threads[free_thread].stack = malloc(UTHREAD_STACK_SIZE);
666: 89 83 04 19 00 00 mov %eax,0x1904(%ebx)
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 3*sizeof(int))) = (int)uthread_exit;
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 2*sizeof(int))) = (int)start_func;
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - sizeof(int))) = (int)arg;
/* changing the state*/
threads[free_thread].state = RUNNABLE_THREAD;
66c: c7 83 08 19 00 00 02 movl $0x2,0x1908(%ebx)
673: 00 00 00
/* creating the stack */
threads[free_thread].stack = malloc(UTHREAD_STACK_SIZE);
/* initializing the stack */
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 3*sizeof(int))) = (int)uthread_exit;
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - 2*sizeof(int))) = (int)start_func;
676: 89 90 f8 0f 00 00 mov %edx,0xff8(%eax)
*((int*)(threads[free_thread].stack + UTHREAD_STACK_SIZE - sizeof(int))) = (int)arg;
67c: 8b 55 0c mov 0xc(%ebp),%edx
67f: 89 90 fc 0f 00 00 mov %edx,0xffc(%eax)
/* changing the state*/
threads[free_thread].state = RUNNABLE_THREAD;
alarm(UTHREAD_QUANTA);
685: c7 04 24 05 00 00 00 movl $0x5,(%esp)
68c: e8 39 06 00 00 call cca <alarm>
return threads[free_thread].thread_id;
691: 8b 83 00 19 00 00 mov 0x1900(%ebx),%eax
697: 83 c4 10 add $0x10,%esp
}
69a: 8d 65 f8 lea -0x8(%ebp),%esp
69d: 5b pop %ebx
69e: 5e pop %esi
69f: 5d pop %ebp