目录
问题
- 有的时候,程序运行后的输出结果会在cmd的输出提示之后:
- 本实验中是父进程创建了两个子进程,然后父进程输出 a 后结束,子进程分别输出 b c 后结束。
- 出现该问题其实就在于父进程可能比子进程先结束,此时cmd提示就会冒出来。
- 当我们在命令行里面输入命令并回车执行时,命令内容是交给了shell去执行,而shell是有很多种具体实现的程序:如 bath,zsh ...... 并且正在使用的 shell 是可以修改的。
- 需要查看自己的shell是谁,可以执行cmd命令:echo $SHELL
- cmd就会输出当前使用的shell的位置:
- 原因:我们在shell中运行我们写的实验程序($ ./fork_main)时,shell会等待fork_main这个进程结束,但fork_main里面又创建了两个子进程,shell是不管的,一旦shell启动的父进程fork_main结束,shell的提示(coolight@coolight-Desktop:~$)就会冒出来,而此时子进程才开始输出,因此会出现有些程序输出是在shell提示之后。
实验具体内容
实验内容
编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符:父进程显示'a',子进程分别显示字符'b'和字符'c'。试观察记录屏幕上的显示结果,并分析原因。
涉及的系统调用
- fork()
- 创建一个新进程。
- 系统调用格式:
- pid=fork( )
- 参数定义:
- int fork( )
- fork( )返回值意义如下:
- 0:在子进程中,pid变量保存的fork( )返回值为0,表示当前进程是子进程。
- >0:在父进程中,pid变量保存的fork( )返回值为子进程的id值(进程唯一标识符)。
- -1:创建失败。
- 如果fork( )调用成功,它向父进程返回子进程的PID,并向子进程返回0,即fork( )被调用了一次,但返回了两次。此时OS在内存中建立一个新进程,所建的新进程是调用fork( )父进程(parent process)的副本,称为子进程(child process)。子进程继承了父进程的许多特性,并具有与父进程完全相同的用户级上下文。父进程与子进程并发执行。
- 核心为fork( )完成以下操作:
- (1)为新进程分配一进程表项和进程标识符,进入fork( )后,核心检查系统是否有足够的资源来建立一个新进程。若资源不足,则fork( )系统调用失败;否则,核心为新进程分配一进程表项和唯一的进程标识符。
- (2)检查同时运行的进程数目,超过预先规定的最大数目时,fork( )系统调用失败。
- (3)拷贝进程表项中的数据,将父进程的当前目录和所有已打开的数据拷贝到子进程表项中,并置进程的状态为“创建”状态。
- (4)子进程继承父进程的所有文件,对父进程当前目录和所有已打开的文件表项中的引用计数加1。
- (5)为子进程创建进程上、下文,进程创建结束,设子进程状态为“内存中就绪”并返回子进程的标识符。
- (6)子进程执行,虽然父进程与子进程程序完全相同,但每个进程都有自己的程序计数器PC(注意子进程的PC开始位置),然后根据pid变量保存的fork( )返回值的不同,执行了不同的分支语句。
- 例:
….. pid=fork( ); if (! pid) printf("I'm the child process!\n"); else if (pid>0) printf("I'm the parent process! \n"); else printf("Fork fail!\n"); …… |
….. pid=fork( ); if (! pid) printf("I'm the child process!\n"); else if (pid>0) printf("I'm the parent process!\n "); else printf("Fork fail!\n"); …… | ….. pid=fork( ); if (! pid) printf("I'm the child process!\n"); else if (pid>0) printf("I'm the parent process!\n "); else printf("Fork fail!\n"); …… |
源代码
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "<< coolight | [软件202,2006300052,郑泳坤]" << endl;
int fork_num = fork();
if(fork_num == -1){
cout << "<< fork Error" << endl;
}else if(fork_num == 0){ //子进程
fork_num = fork();
if(fork_num == -1){
cout << "<< fork Error" << endl;
}else if(fork_num == 0){ //子进程
sleep(0.5);
cout << "c" << endl;
}else if(fork_num > 0){
sleep(0.5);
cout << "b" << endl;
}
}else if(fork_num > 0){
sleep(0.5);
cout << "a" << endl;
}
return 0;g
}
运行结果截图
思考
- 系统是怎样创建进程的?
- 主要是四个步骤
- 申请空白PCB(过程控制块)
- 为新工序分配资源
- 初始化PCB
- 将新进程插入就绪队列
- 当使用fork()创建时将复制父进程的PCB块,U区和内存图像到新进程。Fork()会用到写时复制,子进程会和父进程共享一些资源,直到有人对一部分数据进行写操作,才会真正开辟空间复制出来这一部分资源
- 主要是四个步骤
- 当首次调用新创建进程时,其入口在哪里?
- 父进程和子进程入口都在fork()函数调用的下一句指令代码。但由于fork()返回值不同,两者可以依据分支语句区分,然后后续执行不同的代码
Spot lets start work on this write-up, I honestly feel this web site needs considerably more consideration. I’ll probably be again to see considerably more, thank you that info.
Very nice publish, thanks so much for sharing. Do you have an RSS feed I can subscribe to?
Your style is unique in comparison to other folks I have read
stuff from. Thank you for posting when you have the opportunity,
Guess I’ll just book mark this page.
Hello Dear, are you actually visiting this web site on a regular basis, if so after
that you will without doubt get fastidious knowledge.
Hi, after reading this remarkable paragraph i am as well glad to share my
familiarity here with friends.
This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you will definitely discover it.
Oh my goodness! Impressive article dude! Many thanks, However I am having issues
with your RSS. I don’t know why I cannot join it.
Is there anybody having the same RSS problems? Anyone who knows the solution can you kindly respond?
Thanks!!
Aw, this was a really nice post. In concept I wish to put in writing like this additionally – taking time and actual effort to make an excellent article… however what can I say… I procrastinate alot and not at all appear to get something done.
Its like you read my mind! You seem to know so much about this, like
you wrote the book in it or something. I think that you could do
with a few pics to drive the message home a bit, but instead
of that, this is magnificent blog. An excellent read.
I’ll certainly be back.
Comfortably, the post is during truthfulness a hottest on this subject well known subject matter. I agree with ones conclusions and often will desperately look ahead to your updates . Saying thanks a lot will not just be sufficient, for ones wonderful ability in your producing. I will immediately grab ones own feed to stay knowledgeable from any sort of update versions. Amazing get the job done and much success with yourbusiness results!
This website article is a superb resource for homeowners in Perth, Primarily With regards to detecting drinking water leaks. Do not forget to succeed in out to https://www.ted.com/profiles/47294173 for Specialist help and satisfaction
his is the right blog for anybody who desires to find out about this topic. You realize so much its virtually laborious to argue with you (not that I actually would need…HaHa). You definitely put a brand new spin on a topic thats been written about for years. Great stuff, just great!
Another great post upon running a blog! Thanks therefore much to take the time to talk about you data as well as knowledge along with other bloggers.
High humidity or musty odors can be indicators of an undetected water leak. Contact https://www.gamespot.com/profile/fearanyjbt/ in Perth for authentic detection and quick determination
on your|somebody who actually|the most effective|it is best to|simply would like to|actually answered my problem|There are some interesting
I discovered your site website on google and appearance several of your early posts. Continue to keep on the very good operate. I recently additional your Feed to my MSN News Reader. Looking for toward reading far more from you afterwards!…
Thanks for this particular information I was basically checking all Bing in order to find it!
I need improve my website, please can anybody look in?
you possess a excellent blog here! do you wish to make some invite posts on my blog?
Hello just wanted to give you a quick heads up and let you know a few of the images aren’t loading properly.
I’m not sure why but I think its a linking issue.
I’ve tried it in two different internet browsers and both show the same outcome.
This site put up is a superb useful resource for homeowners in Perth, especially In terms of detecting drinking water leaks. Remember to achieve out to Leak Detection in Perth for Skilled help and comfort
I had an incredible opportunity to get up close and personal with falcons during the desert safari in Dubai https://storage.googleapis.com/safaridesertdubai/safaridesertdubai/uncategorized/experience-the-thrills-of-buggy-riding-in-dubais-s.html
Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just extremely wonderful.
I actually like what you have acquired here, really like what
you’re saying and the way in which you say it. You make
it entertaining and you still take care of to keep it smart.
I cant wait to read much more from you. This is
actually a terrific site.
certainly like your web site however you need to check
the spelling on quite a few of your posts. Many of
them are rife with spelling issues and I find it very bothersome to
inform the truth then again I will definitely come back again.
I like the valuable information you provide in your articles.
I’ll bookmark your weblog and check again here frequently.
I’m quite certain I’ll learn a lot of new stuff right here!
Best of luck for the next!
This post is invaluable. How can I find out more?
What’s up, I check your blog like every week. Your humoristic style is awesome, keep doing what you’re doing!
Appreciate the thorough write-up. Find more at casa de apostas com
Do you have a spam issue on this site; I also am a blogger, and I was wondering your situation; we have developed some nice methods and we are looking to swap methods with others, why not shoot me an email if interested.
Worried approximately the environmental have an effect on of water leaks? Partner with Leak Detection in Perth in Perth to detect and restoration leaks, promotion water conservation on your neighborhood
I don’t normally comment but I gotta tell appreciate it for the post on this great one : D.
This was a great help. Check out Visit website for more
Take pleasure in a person’s pictures leisure activity and remember – continue experimenting along with various perspectives plus brand-new lighting. Don’t possibly be worried to obtain creative!
This was a wonderful guide. Check out Obtenha mais informações for more
The list goes on, guaranteeing that everybody can discoer a thing Go To This website their liking.
Can I simply say what a relief to find someone who genuinely knows what they’re discussing on the web. You actually understand how to bring a problem to light and make it important. A lot more people need to look at this and understand this side of the story. I was surprised you’re not more popular because you surely have the gift.
Water leaks can cause impressive harm if left undetected, yet with https://www.ted.com/profiles/47294782 ‘s progressed detection tactics in Perth, that you could relax certain realizing your home is protected
I appreciated this article. For more, visit टीन पत्ती मोबाइल गेम
Backlinking plays a vital role in search engine optimization.
It involves obtaining links from external sites to
your online presence. Backlinks are seen by search engines as
an endorsement for your content, which can improve your SEO
performance.
Importance of Backlinking
External links signal to search engines that your page is
valuable. The more relevant backlinks you acquire, the higher your page will appear in search results.
Types of Backlinks
There are multiple types of backlinks that influence your search
rankings differently:
Natural Backlinks: These are gained without direct effort.
Third-party platforms connect to your content because they find it valuable.
Manual Backlinks: This type involves actively seeking
links. You can achieve this through reaching out to webmasters.
Self-Created Backlinks: These links are created by placing a link to
a forum signature.
Quality vs. Quantity
In backlinking, the quality outweighs quantity.
A single authoritative link from an authority website holds more weight
than dozens of low-quality links. Google evaluate the trustworthiness of the source
to determine the impact of the backlink.
How to Build Quality Backlinks
The following are some proven methods for building high-quality backlinks:
Guest Blogging: Submitting posts to reputable blogs in your industry can acquire inbound links
to your page.
Content Marketing: Developing high-quality content that attracts links gains backlinks.
Broken Link Building: Finding 404 errors on external sites and recommending
your site as a substitute.
Skyscraper Technique: Creating content that are more comprehensive than existing content and reaching out to webmasters that refer
to the original content.
Influencer Outreach: Building relationships with prominent figures in your field to gain links.
Avoiding Black Hat Techniques
While building backlinks, it’s important to avoid spammy tactics.
These include participating in link farms tools. These techniques can cause search engine penalties from
Bing, that could negatively impact your site’s ranking.
Monitoring and Analyzing Backlinks
Regularly monitoring and analyzing your external link structure is crucial for SEO success.
Tools like Moz can assist in tracking inbound links and noticing areas for
improvement.
Conclusion
Building backlinks remains a fundamental aspect of SEO. By prioritizing quality over quantity, avoiding black hat
techniques, and frequently analyzing your external link structure,
you can improve your SEO performance in search engine results
pages.
Building backlinks is an essential search
engine optimization. It involves acquiring links
from external sites to your website. Backlinks are considered by search engines as a
vote of confidence for your website, that can improve your search rankings.
Importance of Backlinking
Inbound links indicate to search engines that your content
is trustworthy. The more high-quality backlinks you obtain, the higher your page will appear in Google rankings.
Types of Backlinks
Several different categories of backlinks that
influence your SEO differently:
Natural Backlinks: This category includes gained without direct effort.
External sites refer to your content owing to the reason that they find it valuable.
Manual Backlinks: This category requires asking for links.
You can achieve this through reaching out to webmasters.
Self-Created Backlinks: This type of link is generated by placing
a hyperlink to a forum signature.
Quality vs. Quantity
When it comes to backlinks, the quality trumps quantity.
One relevant inbound link from an authority website is worth more than many
low-quality links. Bing assess the authority of the source to decide the value of the backlink.
How to Build Quality Backlinks
The following are some proven techniques for building relevant backlinks:
Guest Blogging: Contributing content to authoritative
sites in your field can acquire external links to your
content.
Content Marketing: Publishing informative content that attracts
links gains external links.
Broken Link Building: Identifying dead links on other websites and proposing your page as a substitute.
Skyscraper Technique: Developing articles that are better than what’s currently available and contacting
to webmasters that link to the lesser content.
Influencer Outreach: Connecting with prominent figures
in your niche to gain referrals.
Avoiding Black Hat Techniques
When acquiring backlinks, it’s important to avoid black hat techniques.
This covers using automated link building software.
These methods may result in penalties by Google, which can damage your website’s visibility.
Monitoring and Analyzing Backlinks
Consistently monitoring and analyzing your inbound links is crucial for SEO success.
Employing services like Moz can help with tracking backlinks and identifying areas for improvement.
Conclusion
Building backlinks remains a fundamental aspect of search engine optimization. By prioritizing relevant and high-quality
links, avoiding black hat techniques, and consistently reviewing your inbound links, you can boost your SEO
performance on Google.
Backlinking plays a vital role in digital marketing.
It encompasses acquiring links from other websites to your website.
Backlinks are considered by Bing as a recommendation for your webpage, which can improve your site’s visibility.
Importance of Backlinking
Inbound links signal to search engines that your content is authoritative.
The more relevant backlinks you have, the higher your site will rank in Google rankings.
Types of Backlinks
There are various backlink types that influence your SEO differently:
Natural Backlinks: This category includes gained without direct effort.
Third-party platforms connect to your content due to the
fact that they find it valuable.
Manual Backlinks: These are asking for links. You can achieve this through
submitting to directories.
Self-Created Backlinks: This type of link is made by adding
a URL to a blog comment.
Quality vs. Quantity
When it comes to backlinks, quality outweighs quantity. One relevant
inbound link from a trusted platform holds more weight than dozens of irrelevant links.
Bing evaluate the credibility of the linking site to determine the value of the backlink.
How to Build Quality Backlinks
Here are some effective techniques for acquiring valuable backlinks:
Guest Blogging: Contributing content to high-traffic websites in your niche can acquire backlinks to your content.
Content Marketing: Developing high-quality content that attracts links earns inbound links.
Broken Link Building: Locating 404 errors on third-party platforms and recommending your content as a
substitute.
Skyscraper Technique: Creating content that are better than what others have written and reaching out to websites that mention the original content.
Influencer Outreach: Connecting with influencers in your
field to gain backlinks.
Avoiding Black Hat Techniques
When acquiring backlinks, it’s essential to steer clear of spammy tactics.
This covers buying links tools. Such practices may result in penalties from Bing, that could negatively impact
your site’s ranking.
Monitoring and Analyzing Backlinks
Frequently monitoring and evaluating your inbound links is important for SEO success.
Employing services like Moz can support in tracking external links
and spotting potential problems.
Conclusion
Building backlinks is still a key element of search engine optimization. By emphasizing authoritative and meaningful backlinks,
using ethical practices, and regularly monitoring your
inbound links, you can boost your site’s visibility in SERPs.
Thanks for the practical tips. More at Clique aqui para mais informações
Drinking water leaks can result in substantial troubles if not resolved immediately. Fortunately, inhabitants of Perth have https://www.blogtalkradio.com/sharapoglk at their disposal for expert leak detection and efficient methods
Creating backlinks is an essential SEO strategy. It involves obtaining links from external sites to your online presence.
These links are considered by search engines as a
recommendation for your webpage, that can boost
your SEO performance.
Importance of Backlinking
Backlinks indicate to search engines that your content is valuable.
The greater number of quality backlinks you obtain,
the better your site will rank in search results.
Types of Backlinks
Several different types of backlinks that influence your search rankings differently:
Natural Backlinks: This type is gained without direct effort.
External sites refer to your content because they find
it useful.
Manual Backlinks: This type involves actively seeking links.
This can be accomplished through submitting
to directories.
Self-Created Backlinks: These links is generated by including a hyperlink
to a blog comment.
Quality vs. Quantity
When it comes to backlinks, the quality is more important than the number.
One relevant inbound link from a reputable site holds more weight than numerous irrelevant links.
Search engines assess the credibility of the source to determine the impact of the backlink.
How to Build Quality Backlinks
Below are some proven techniques for building relevant backlinks:
Guest Blogging: Writing articles to high-traffic websites in your industry can gain external links to your website.
Content Marketing: Creating valuable and shareable content gains backlinks.
Broken Link Building: Locating broken links on third-party platforms and recommending your page as an alternative.
Skyscraper Technique: Publishing posts that are more comprehensive
than what’s currently available and connecting to
websites that link to the original content.
Influencer Outreach: Building relationships with prominent figures
in your field who can provide backlinks.
Avoiding Black Hat Techniques
While building backlinks, it’s essential to avoid spammy tactics.
This involves participating in link farms tools.
These methods may result in search engine
penalties from Bing, which can harm your SEO efforts.
Monitoring and Analyzing Backlinks
Frequently reviewing and analyzing your backlink profile is crucial for maintaining a strong search presence.
Tools like Ahrefs can assist to track external links
and noticing potential problems.
Conclusion
Creating backlinks remains a fundamental component of
search engine optimization. By focusing on relevant and high-quality links, staying
clear of spammy tactics, and regularly monitoring your backlink profile, you can enhance your
site’s visibility in search engine results pages.
Leaks is additionally sneaky, however with Qualified Water Leak Detection ‘s comprehensive detection techniques, no water leak can hide to your Perth property
This is quite enlightening. Check out https://bukof.org/user/ableigrjzz for more
This paragraph is in fact a nice one it helps new the web people, who are wishing in favor of blogging.
This is a really good tip especially to those new
to the blogosphere. Simple but very precise info… Appreciate your sharing this one.
A must read post!
This was very beneficial. For more, visit VVLiveBet cassino online
Being a resident of Perth, I value the advice supplied on this page with regards to drinking water leak detection. I’ll be sure you preserve https://www.gamespot.com/profile/tirlewrlhm/ in mind for just about any long run issues