الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤال في جزئية في لغة السي

مغلق
بدأه EncrYpted-ErrOr في 26 أغسطس 2007 · 4 رد · 935 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم شباب

ان شاء الله الكل بخير :)

عندي سؤال في لغة السي وحاولت كثيرا البحث عن الحل المناسب الصراحة ما وصلت للحل

والسؤال هو

عندنا الدالة system

مثلا قمت بكتابه هذا السطر

بمجرد كتابة

system("which gcc");

بعد الترجمة ينفذ هذا الامر في نظام اللينوكس ويعطيني الناتج

/usr/bin/gcc

والسؤال هو اريد ان ناتج الامر لا يظهر في التريمنال ويقوم كتابه بدل الناتج

gcc installed اذا كان الناتج هو

/usr/bin/gcc

اما اذا كان غير ذلك يكتب

gcc not installed

الصراحة حاولت بطرق عدة تطبيق ما اقوله ولم اوصل لنتيجة للأسف :rolleyes:

منتظر ردكم

وان شاء الله خير

تحياتي :)

#2

السلام عليكم و رحمة اللهالحقيقة سؤالك هذا جميل, لاجيب عليههذه بعض المعلومات نتيجة man 3 system DESCRIPTION system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.RETURN VALUE The value returned is -1 on error (e.g. fork() failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127). If the value of command is NULL, system() returns non-zero if the shell is available, and zero if not. system() does not affect the wait status of any other children.من الواضح ان ال process الجديدة التى تم انشاءها "فى هذه الحالة which" تستعمل نفس ال input pipe و output pipeيمكنك اعادة توجيه standard output الى pipe ثم القراءه منهكتبت لك هذا الكود هو يعمل رغم ان فيه الكثير من الاخطاءو هو يحتاج الى الكثير من ال validationارجو ان تستفيد منهnote : this code create a new process to and connect to it throughthe pipe, you may notice that the usage of a thread may be cheaper.Yes, it is true, using threads is cheaper, but that is left to you as an exercise #include "stdio.h"#include "stdlib.h"int main(){ int pipe_fds[2]; int read_fd , write_fd; pipe(pipe_fds); read_fd = pipe_fds[0]; write_fd= pipe_fds[1]; if (fork() == 0 ) { dup2( write_fd , 1 ); system("which gcc"); exit(0); } char buffer[1024]; char* offset; int r = 1; offset = buffer; r = read ( read_fd , offset , 1024); printf( "the output is **********************\n %s \n *******************************" , buffer);}

السلام عليكم و رحمة اللهالحقيقة سؤالك هذا جميل, لاجيب عليههذه بعض المعلومات نتيجة man 3 system DESCRIPTION system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.RETURN VALUE The value returned is -1 on error (e.g. fork() failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127). If the value of command is NULL, system() returns non-zero if the shell is available, and zero if not. system() does not affect the wait status of any other children.من الواضح ان ال process الجديدة التى تم انشاءها "فى هذه الحالة which" تستعمل نفس ال input pipe و output pipeيمكنك اعادة توجيه standard output الى pipe ثم القراءه منهكتبت لك هذا الكود هو يعمل رغم ان فيه الكثير من الاخطاءو هو يحتاج الى الكثير من ال validationارجو ان تستفيد منهnote : this code create a new process to and connect to it throughthe pipe, you may notice that the usage of a thread may be cheaper.Yes, it is true, using threads is cheaper, but that is left to you as an exercise

system.pdf

system.c.pdf

system.pdf

system.c.pdf

#3

عذرا اخي الصراحة ما فهمتك

ممكن توضيح اكثر

وشكرا على اهتمامك :)

#4

السلام عليكم و رحمة الله

الحقيقة سؤالك هذا جميل, لاجيب عليه

هذه بعض المعلومات نتيجة

man 3 system

DESCRIPTION

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been

completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

RETURN VALUE

The value returned is -1 on error (e.g. fork() failed), and the return status of the command otherwise. This latter

return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status).

In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).

If the value of command is NULL, system() returns non-zero if the shell is available, and zero if not.

system() does not affect the wait status of any other children.

من الواضح ان ال process الجديدة التى تم انشاءها "فى هذه الحالة which"

تستعمل نفس ال input pipe و output pipe

يمكنك اعادة توجيه standard output الى pipe ثم القراءه منه

كتبت لك هذا الكود هو يعمل رغم ان فيه الكثير من الاخطاء

و هو يحتاج الى الكثير من ال validationارجو ان تستفيد منه

note : this code create a new process to and connect to it through

the pipe, you may notice that the usage of a thread may be cheaper.

Yes, it is true, using threads is cheaper, but that is left to you as an exercise

system.pdf

system.c.pdf

#5

استخدم access() عشان تتاكد من مكانه

ممكن تستخدم عدة مسارات وتدمجها في if , else flow وتتاكد من وجوده

مثال

access("/usr/bin/gcc",F_OK)

لو موجود return 0 لو لا -1

man access

هذا الموضوع مغلق.

مواضيع مشابهة