עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

שאלה בנוגע לעבודה עם זמנים (ספירה של שניות) בשפת C ***עריכה : שאלה נוספת

Featured Replies

פורסם

שלום ,

בפונקציה, שצירפתי את הקוד שלה, אני אמור לשלוח סיגנל SIGTERM כדי לסגור תהליך מסוים. (מספרי התהליכים נשמרים ברשימה ואני צריך לסגור את כל התהליכים ברשימה)

אם התהליך התעלם מהסיגנל אז אחרי 5 שניות אני שולח סיגנל SIGKILL .

מה שקורה בפועל, שעם הכניסה לפונקציה יש המתנה של 5 שניות ואז הדפסה ברצף של כל שורות ההדפסה הרלוונטיות.

אני אשמח אם מישהו יוכל להסביר לי למה אין המתנה של 5 שניות אחרי הprintf הראשון.

תודה

Status free_all_processes()
{
int i = 1,pid;
time_t start,end;
double elapsed;

Process* pPtr = pList.root;

while (pPtr != NULL)
{
elapsed = 0;
pid = pPtr->pid;
kill(pid,SIGTERM);
printf("[%d] %s ‫‪– Sending SIGTERM‬‬...",i++,pPtr->command);

time(&start);
while (elapsed < 5)
{
time(&end);
elapsed = difftime(end,start);
}
if (findProcess(pid) == 1)
{
printf("‫...‪(5 sec passed) Sending SIGKILL‬‬");
kill(pid,SIGKILL);
}

printf("Done.\n");
pPtr = pPtr->next;
}

return QUIT;
}

פורסם

אתה צריך לדאוג לרוקן את באפר הפלט אחרי ההדפסה ע"י קריאה ל-(fflush(stdout.

חוץ מזה, זו דרך ממש גרועה לחכות 5 שניות, כי היא סתם זוללת זמן של המעבד. במקום זה תשתמש בפונקציה sleep.

פורסם
  • מחבר

תודה רבה, הסתדר

פורסם
  • מחבר

בתהליך הראשון שאני מנסה להרוג אכן יש המתנה של 5 שניות אבל באיטרציות הבאות של הלולאה השורות נדפסה מודפסות אחרת אחרי השנייה ללא המתנה..


Status free_all_processes()
{
int i = 1,pid;

Process* pPtr = pList.root;

/* For each process in the process list, send a signal SIGTERM. If the process does not
* react, there will be sent another signal(SIGKILL) */
while (pPtr != NULL)
{

pid = pPtr->pid;
kill(pid,SIGTERM);
printf("[%d] %s ‫‪– Sending SIGTERM‬‬...",i++,pPtr->command);
fflush(stdout);
sleep(5);
if (findProcess(pid) == 1) /* if the process exists in the process list - kill it! */
{
kill(pid,SIGKILL);
printf("‫...‪(5 sec passed) Sending SIGKILL‬‬");
fflush(stdout);
}
printf("Done.\n");
fflush(stdout);

pPtr = pPtr->next;
}

return QUIT;
}

זה הקוד אחרי השינוי

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.