QT New Process working directory settings (working directory is actually inherited by the quilt process, but can also be set)

Source: Internet
Author: User

After a trial, QT initiates a new process, the working directory of the process is inherited from the parent process, either through start or startdetached.

In fact, for the Linux system, the QT bottom should also call fork, exec and other functions, for fork, see Apue Chinese version of the third version, the following analysis:

There are two common scenarios for processing file descriptors after F o r K:
(1) The parent process waits for the child process to complete. In this case, the parent process does not need to do any processing of its descriptors. When Wahabbi
After the process terminates, the file offset of any shared descriptor for which it has read and write has been updated accordingly.
(2) The parent and child processes each execute different program segments. In this case, after F o r K, the parent and child processes are closed separately
They do not need to use file descriptors and do not interfere with the file descriptors used by the other party. This approach is in the network service process
Often used.
In addition to opening the file, many of the other properties of the parent process are inherited by the child process:
• Actual user i d, actual group I d, active user I d, effective group I d.
• Add Group I D.
• Process Group I D.
• Conversation period I D.
• Control terminals.
• Set-user-I d flag and set-group-i-D flag.
current working directory.
• root directory.
• File mode to create a masking word.
• Signal shielding and alignment.
• Close flag on execution of any open file descriptor.
Environment
• Shared storage segments that are connected.
• Resource limitations.
The differences between parent and child processes are:
The return value of the fork.
• Process I D.
• Different parent process I D.
• Sub-process T M s _ u t i m e, T M s _ s t i m E, T M S _ c u t i m E and T M s _ u s t i m e set to 0.
• The parent process sets the lock that the child process does not inherit.
• Pending alarms for child processes are cleared.
• The pending signal set of the child process is set to the empty.

From the above analysis, the working directory is actually inherited by the quilt process. Personal understanding of Qt setworkingdirectory equivalent to: Fork after the chdir, and QT new process start or startdetached equivalent to one of the exec family.

To talk back to Qt, without setting the working directory of the child process, if the new process code inside the relative path is used, then the relative path is relative to the parent process's working directory, rather than to start or startdetached the first parameter inside the path. So the problem is, if the new process is to use a relative path to read and write the configuration file, this configuration file read and write will be in the parent process's working directory, which is undoubtedly a big problem.

In response to the above questions, individuals conducted the following experiments and summarized.

Both ways can get the path of the current process
QString path = Qapplication::applicationfilepath ();
int index = Path.lastindexof ("/");
Path = Path.mid (0, index);
Qdebug () << "Applicationfilepath" << path;
Qdir dir;
Path=dir.currentpath ();
Qdebug () << "Current path" << path;


Construct a new process
Qprocess *updateprocess = new Qprocess (this);

Specify working directory
QString Workingdir = "/home/mohanhui/work/tmp/test/qt5.3.2-64/softwarecenter/"
"Build-softwarecenter-desktop_qt_5_3_gcc_64bit-debug";

Specify the path to the executable program
QString program = Workingdir + "/softwarecentertest";

/*1.setcurrent
* Child process Inheritance
* Phenomenon: The child process inherits the working directory, and the sub-process uses "./" that is equivalent to the directory specified by this setcurrent.
* When a child process reads and writes files using "./", it takes effect in the directory specified by this setcurrent.
* But the child process printing WorkingDirectory The result is "" empty, do not know if there will be other problems.
*/
#if 0
Change the working directory of the parent process
Qdir::setcurrent (Workingdir);
Start a new process
Updateprocess->startdetached (program);
Qdebug () << "New working Directory" << Updateprocess->workingdirectory ();
Return
#endif

/* 2. Don't do anything.
* Phenomenon: The child process print WorkingDirectory is "" empty. The child process inherits the working directory of the parent process.
* The sub-process uses "./" That is, the directory specified in the parent process.
* The child process reads and writes files using "./" When reading and writing the configuration file, it takes effect in the parent process's directory, so the child process is not working properly.
*/
#if 0
Start a new process
Updateprocess->startdetached (program);
Qdebug () << "New working Directory" << Updateprocess->workingdirectory ();
Return
#endif

/* 3. Call Setworkingdirectory before the child process starts
* Phenomenon: Seemingly use relative path, the program will exit itself, the reason is unknown. (Maybe it's about my system,
* No attempt on other systems)
*/
#if 0
Set child process working directory
Updateprocess->setworkingdirectory (Workingdir);
Start a new process
Updateprocess->startdetached (program);
Qdebug () << "New working Directory" << Updateprocess->workingdirectory ();
Return
#endif

/* 4. When the child process is created, specify the working directory, such as newprocess->startdetached (Program,qstringlist (), workingdir);
* Phenomenon: Very good, the parent process is separate from the working directory of the child process and does not affect each other. Parent process uses relative directory
* When the working directory is relative to the parent process, the child process uses a relative path when it is relative to the working directory of the child process.
* */
#if 0
Start a new process
Updateprocess->startdetached (Program,qstringlist (), workingdir);
Qdebug () << "New working Directory" << Updateprocess->workingdirectory ();
Return
#endif

http://blog.csdn.net/u013281495/article/details/51086943

QT New Process working directory settings (working directory is actually inherited by the quilt process, but can also be set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.